Skip to content

Commit

Permalink
improve day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
wowkster committed Dec 8, 2023
1 parent 18af967 commit f3cd946
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 9 additions & 5 deletions src/bin/08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use nom::{
multi::{count, many1, separated_list0},
IResult,
};
use rayon::prelude::*;

advent_of_code_2023::solution!(8);

Expand All @@ -26,15 +27,18 @@ pub fn part_2(input: &str) -> Option<u64> {

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(
Expand Down

0 comments on commit f3cd946

Please sign in to comment.