From f3cd94690f042999b03511d19af530239732073d Mon Sep 17 00:00:00 2001 From: Adrian Wowk Date: Fri, 8 Dec 2023 13:34:30 -0500 Subject: [PATCH] improve day 8 --- README.md | 2 +- src/bin/08.rs | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1b80fef..e11a881 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ The following benchmarks were created on a Macbook Pro with an M2 Pro processor: | [Day 5](https://github.com/wowkster/advent-of-code-2023/blob/main/src/bin/05.rs) | `19.5µs` | `55.2µs` | | [Day 6](https://github.com/wowkster/advent-of-code-2023/blob/main/src/bin/06.rs) | `0.21µs` | `0.25µs` | | [Day 7](https://github.com/wowkster/advent-of-code-2023/blob/main/src/bin/07.rs) | `3.87ms` | `5.12ms` | -| [Day 8](https://github.com/wowkster/advent-of-code-2023/blob/main/src/bin/08.rs) | `0.619ms` | `3.01ms` | +| [Day 8](https://github.com/wowkster/advent-of-code-2023/blob/main/src/bin/08.rs) | `619.5µs` | `796.6µs` | ## Project Structure diff --git a/src/bin/08.rs b/src/bin/08.rs index ca17f0b..f663d17 100644 --- a/src/bin/08.rs +++ b/src/bin/08.rs @@ -9,6 +9,7 @@ use nom::{ multi::{count, many1, separated_list0}, IResult, }; +use rayon::prelude::*; advent_of_code_2023::solution!(8); @@ -26,15 +27,18 @@ pub fn part_2(input: &str) -> Option { assert_eq!(input, ""); - network - // Get all node ids - .keys() - // Find all the starting nodes + let steps = network + .par_iter() + // Get all node ids (no method for getting keys in parallel) + .map(|(k, _)| k) + // Find all the starting node ids .filter(|k| k.ends_with('A')) // Find the individual path for each node .map(|node| count_steps(&instructions, &network, node, |n| n.ends_with('Z'))) // Find the LCM of all the paths to find the total step count - .reduce(num::integer::lcm) + .reduce(|| 1, num::integer::lcm); + + Some(steps) } fn count_steps(