Skip to content

Commit

Permalink
minor optimizations for p2. vec (with dfs) is faster than vecdeque
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 19, 2023
1 parent 03997f2 commit 65121b4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
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.4µs` | `2.5µs` |
| [Day 19](./src/bin/19.rs) | `165.3µs` | `642.5µs` |
| [Day 19](./src/bin/19.rs) | `167.5µs` | `616.8µs` |

**Total: 14.89ms**
**Total: 14.86ms**
<!--- benchmarking table --->

---
Expand Down
12 changes: 6 additions & 6 deletions src/bin/19.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ pub fn part_two(input: &str) -> Option<u64> {

// BFS until we find accept nodes. Each path to an accept node results
// in a volume of possible ratings. The union of those volumes is our answer.
let mut queue = VecDeque::new();
queue.push_back(("in", PartFilter::new(1, 4000)));
let mut accept_volumes = Vec::new();
while let Some((cur, filter)) = queue.pop_front() {
let mut stack = Vec::with_capacity(1000);
stack.push(("in", PartFilter::new(1, 4000)));
let mut accept_volumes = Vec::with_capacity(1000);
while let Some((cur, filter)) = stack.pop() {
let workflow = &workflows[cur];
let mut workflow_filter = Some(filter);
for rule in &workflow.rules {
Expand All @@ -49,7 +49,7 @@ pub fn part_two(input: &str) -> Option<u64> {
{
match rule.destination {
Destination::Accept => accept_volumes.push(new_filter),
Destination::Next(d) => queue.push_back((d, new_filter)),
Destination::Next(d) => stack.push((d, new_filter)),
Destination::Reject => {}
}
}
Expand All @@ -62,7 +62,7 @@ pub fn part_two(input: &str) -> Option<u64> {
if let Some(filter) = workflow_filter {
match workflow.default_rule {
Destination::Accept => accept_volumes.push(filter),
Destination::Next(d) => queue.push_back((d, filter)),
Destination::Next(d) => stack.push((d, filter)),
Destination::Reject => {}
}
}
Expand Down

0 comments on commit 65121b4

Please sign in to comment.