From 0009e04de62d534b71f4e7b56d0b93889e54f101 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 8 Dec 2023 07:59:19 +0100 Subject: [PATCH] Day08 - part 1 --- src/day08.rs | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 + 2 files changed, 128 insertions(+) create mode 100644 src/day08.rs diff --git a/src/day08.rs b/src/day08.rs new file mode 100644 index 0000000..ca3f181 --- /dev/null +++ b/src/day08.rs @@ -0,0 +1,126 @@ +use std::io::Read; +use std::error::Error; +use std::fs::File; + +use std::collections::HashMap; + +pub fn run(input_file: &str) -> Result<(), Box> { + let mut f = File::open(input_file)?; + let mut input = String::new(); + f.read_to_string(&mut input)?; + + let res = run_part1(&input)?; + println!("{res}"); + + let res = run_part2(&input)?; + println!("{res}"); + + Ok(()) +} + +#[derive(Debug)] +struct Puzzle { + instructions: Vec, + nodes: HashMap, +} + +impl Puzzle { + pub fn new(input: &str) -> Self { + let (str_instructions, str_nodes) = input.split_once("\n\n").unwrap(); + + let instructions: Vec = str_instructions.chars().collect(); + let nodes: HashMap = str_nodes.split('\n') + .filter(|l| !l.is_empty()) + .map(|l| { + let (node, value) = l.split_once(" = ").unwrap(); + let x: &[_] = &['(',')']; + let (left, right) = value.trim_matches(x).split_once(", ").unwrap(); + (node.to_string(), (left.to_string(), right.to_string())) + }) + .collect(); + + Self { + instructions, + nodes, + } + } +} + +fn run_part1(input: &str) -> Result> { + println!("Running day08 - part 1"); + let puzzle = Puzzle::new(input); + + let mut step = 0; + let mut i = 0; + let mut found_ZZZ = false; + let mut node: String = String::from("AAA"); + + while !found_ZZZ { + if i >= puzzle.instructions.len() { + i = 0; + } + + let new_node = match puzzle.instructions[i] { + 'L' => &puzzle.nodes.get(&node).unwrap().0, + 'R' => &puzzle.nodes.get(&node).unwrap().1, + _ => unreachable!(), + }; + + node = new_node.to_string(); + if node == "ZZZ".to_string() { + found_ZZZ = true; + } + i += 1; + step += 1; + } + + let res = step; + Ok(res) +} + +fn run_part2(input: &str) -> Result> { + println!("Running day08 - part 2"); + let res = 0; + Ok(res) +} + +#[cfg(test)] +mod tests { + use super::*; + + static TEXT_INPUT: &str = ""; + + #[test] + fn day08_part1_example1() { + let input = "\ +RL + +AAA = (BBB, CCC) +BBB = (DDD, EEE) +CCC = (ZZZ, GGG) +DDD = (DDD, DDD) +EEE = (EEE, EEE) +GGG = (GGG, GGG) +ZZZ = (ZZZ, ZZZ)"; + let res = run_part1(&input); + assert_eq!(2, res.unwrap()); + } + + #[test] + fn day08_part1_example2() { + let input = "\ +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ)"; + let res = run_part1(&input); + assert_eq!(6, res.unwrap()); + } + + #[test] + fn day08_part2() { + let res = run_part2(TEXT_INPUT); + assert_eq!(0, res.unwrap()); + } +} diff --git a/src/main.rs b/src/main.rs index d4ac3b3..a441260 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ pub mod day04; pub mod day05; pub mod day06; pub mod day07; +pub mod day08; fn main() { let args: Vec = env::args().collect(); @@ -35,6 +36,7 @@ fn run(day: &str, input_file: &str) -> Result<(), Box> { "day05" => day05::run(input_file)?, "day06" => day06::run(input_file)?, "day07" => day07::run(input_file)?, + "day08" => day08::run(input_file)?, _ => return Err(format!("unknown day \"{day}\"").into()), } Ok(()) -- 2.39.5