]> aoc.elinar.fr Git - aoc_2023/commitdiff
Day08 - part 1
authoralex <null>
Fri, 8 Dec 2023 06:59:19 +0000 (07:59 +0100)
committeralex <null>
Fri, 8 Dec 2023 06:59:19 +0000 (07:59 +0100)
src/day08.rs [new file with mode: 0644]
src/main.rs

diff --git a/src/day08.rs b/src/day08.rs
new file mode 100644 (file)
index 0000000..ca3f181
--- /dev/null
@@ -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<dyn Error>> {
+    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<char>,
+    nodes: HashMap<String, (String, String)>,
+}
+
+impl Puzzle {
+    pub fn new(input: &str) -> Self {
+        let (str_instructions, str_nodes) = input.split_once("\n\n").unwrap();
+
+        let instructions: Vec<char> = str_instructions.chars().collect();
+        let nodes: HashMap<String, (String, String)> = 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<u32, Box<dyn Error>> {
+    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<u32, Box<dyn Error>> {
+    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());
+    }
+}
index d4ac3b35a0634f5d0cfa2f24db2869727cda0966..a441260647919a34cdc4b755743229f74517934b 100644 (file)
@@ -10,6 +10,7 @@ pub mod day04;
 pub mod day05;
 pub mod day06;
 pub mod day07;
+pub mod day08;
 
 fn main() {
     let args: Vec<String> = env::args().collect();
@@ -35,6 +36,7 @@ fn run(day: &str, input_file: &str) -> Result<(), Box<dyn Error>> {
         "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(())