Skip to content

Commit

Permalink
more day 19 parsing optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 19, 2023
1 parent 00c6c09 commit 588feeb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ num-integer = "0.1"
thiserror = "1.0.50"
either = "1.9.0"
tinyvec = "1.6.0"
fxhash = "0.2.1"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 16](./src/bin/16.rs) | `58.3µs` | `1.7ms` |
| [Day 17](./src/bin/17.rs) | `1.6ms` | `3.7ms` |
| [Day 18](./src/bin/18.rs) | `2.2µs` | `2.4µs` |
| [Day 19](./src/bin/19.rs) | `106.0µs` | `113.0µs` |
| [Day 19](./src/bin/19.rs) | `84.3µs` | `91.4µs` |

**Total: 14.30ms**
**Total: 14.25ms**
<!--- benchmarking table --->

---
Expand Down
11 changes: 4 additions & 7 deletions src/bin/19.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;

use fxhash::FxHashMap;
use itertools::Itertools;

advent_of_code::solution!(19);
Expand Down Expand Up @@ -80,8 +79,9 @@ pub fn part_two(input: &str) -> Option<u64> {

fn parse_workflows<'a>(input: &'a [u8]) -> (Vec<Workflow>, u16) {
let mut next_id = 0;
let mut name_to_id = HashMap::new();
let mut workflows = Vec::new();
let num_workflows = input.iter().filter(|&&ch| ch == b'\n').count() + 1;
let mut name_to_id = FxHashMap::with_capacity_and_hasher(num_workflows, Default::default());
let mut workflows = vec![Workflow::default(); num_workflows];
let mut start = 0;
for line in input.split(|&ch| ch == b'\n') {
let rule_start = line.iter().position(|&ch| ch == b'{').unwrap();
Expand Down Expand Up @@ -132,9 +132,6 @@ fn parse_workflows<'a>(input: &'a [u8]) -> (Vec<Workflow>, u16) {
rules,
default_rule,
};
if name_id as usize >= workflows.len() {
workflows.resize_with(name_id as usize + 1, Workflow::default);
}
workflows[name_id as usize] = workflow;
}
(workflows, start)
Expand Down

0 comments on commit 588feeb

Please sign in to comment.