diff options
author | Jesse Luehrs <doy@tozt.net> | 2023-12-09 18:55:27 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2023-12-09 18:55:27 -0500 |
commit | 97296bee681f3ab61313c02ad394fd73e46d2f0a (patch) | |
tree | ab2cf00a5d3dbd2fc712750a56be6af49b9c0305 /src/bin | |
parent | 33879e768c2eb83659d239bc1b927ed2203a2859 (diff) | |
download | advent-of-code-97296bee681f3ab61313c02ad394fd73e46d2f0a.tar.gz advent-of-code-97296bee681f3ab61313c02ad394fd73e46d2f0a.zip |
day 8 part 1
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/2023/day8.rs | 73 | ||||
-rw-r--r-- | src/bin/2023/main.rs | 2 |
2 files changed, 75 insertions, 0 deletions
diff --git a/src/bin/2023/day8.rs b/src/bin/2023/day8.rs new file mode 100644 index 0000000..5b43ee5 --- /dev/null +++ b/src/bin/2023/day8.rs @@ -0,0 +1,73 @@ +#![allow(dead_code)] +#![allow(unused_variables)] + +use advent_of_code::prelude::*; + +#[derive(Clone, Copy)] +enum Direction { + Left, + Right, +} + +pub struct Network { + directions: Vec<Direction>, + graph: HashMap<String, (String, String)>, +} + +pub fn parse(fh: File) -> Result<Network> { + let mut lines = parse::raw_lines(fh); + let directions = lines.next().unwrap(); + lines.next().unwrap(); + Ok(Network { + directions: directions + .chars() + .map(|c| { + if c == 'L' { + Direction::Left + } else { + Direction::Right + } + }) + .collect(), + graph: lines + .map(|line| { + let cap = regex_captures!(r"(\w+) = \((\w+), (\w+)\)", &line) + .unwrap(); + (cap[1].to_string(), (cap[2].to_string(), cap[3].to_string())) + }) + .collect(), + }) +} + +pub fn part1(network: Network) -> Result<i64> { + let mut vertex = "AAA".to_string(); + let mut distance = 0; + + while vertex != "ZZZ" { + let next = network.graph[&vertex].clone(); + vertex = match network.directions[distance % network.directions.len()] + { + Direction::Left => next.0, + Direction::Right => next.1, + }; + distance += 1; + } + + Ok(distance.try_into().unwrap()) +} + +pub fn part2(network: Network) -> Result<i64> { + todo!() +} + +#[test] +fn test() { + assert_eq!( + part1(parse(parse::data(2023, 8).unwrap()).unwrap()).unwrap(), + 11309 + ); + assert_eq!( + part2(parse(parse::data(2023, 8).unwrap()).unwrap()).unwrap(), + 0 + ); +} diff --git a/src/bin/2023/main.rs b/src/bin/2023/main.rs index 228cd57..569886c 100644 --- a/src/bin/2023/main.rs +++ b/src/bin/2023/main.rs @@ -18,6 +18,7 @@ mod day4; mod day5; mod day6; mod day7; +mod day8; // NEXT MOD #[paw::main] @@ -31,6 +32,7 @@ fn main(opt: Opt) -> Result<()> { 5 => advent_of_code::day!(2023, opt.day, opt.puzzle, day5), 6 => advent_of_code::day!(2023, opt.day, opt.puzzle, day6), 7 => advent_of_code::day!(2023, opt.day, opt.puzzle, day7), + 8 => advent_of_code::day!(2023, opt.day, opt.puzzle, day8), // NEXT PART _ => panic!("unknown day {}", opt.day), } |