From 65121b48bf05114eebe3fb3a3553afc8d4c70d04 Mon Sep 17 00:00:00 2001 From: Kevin Caffrey Date: Tue, 19 Dec 2023 11:47:36 -0500 Subject: [PATCH] minor optimizations for p2. vec (with dfs) is faster than vecdeque --- README.md | 4 ++-- src/bin/19.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 853c185..ad31bb6 100644 --- a/README.md +++ b/README.md @@ -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** --- diff --git a/src/bin/19.rs b/src/bin/19.rs index 04661e7..99f5b62 100644 --- a/src/bin/19.rs +++ b/src/bin/19.rs @@ -37,10 +37,10 @@ pub fn part_two(input: &str) -> Option { // 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 { @@ -49,7 +49,7 @@ pub fn part_two(input: &str) -> Option { { 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 => {} } } @@ -62,7 +62,7 @@ pub fn part_two(input: &str) -> Option { 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 => {} } }