From 38ccaeef6e2cb105b847e2f2292716b39b05ab30 Mon Sep 17 00:00:00 2001 From: BlackDemonFire Date: Tue, 12 Dec 2023 08:24:46 +0100 Subject: [PATCH] Day 8 --- Cargo.lock | 8 ++++++ Cargo.toml | 2 ++ d08t1/Cargo.toml | 8 ++++++ d08t1/demo_input.txt | 5 ++++ d08t1/src/main.rs | 40 +++++++++++++++++++++++++++ d08t2/Cargo.toml | 8 ++++++ d08t2/demo_input.txt | 10 +++++++ d08t2/src/main.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 146 insertions(+) create mode 100644 d08t1/Cargo.toml create mode 100644 d08t1/demo_input.txt create mode 100644 d08t1/src/main.rs create mode 100644 d08t2/Cargo.toml create mode 100644 d08t2/demo_input.txt create mode 100644 d08t2/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 63a06ff..d7215f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,3 +57,11 @@ version = "0.1.0" [[package]] name = "d07t2" version = "0.1.0" + +[[package]] +name = "d08t1" +version = "0.1.0" + +[[package]] +name = "d08t2" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index cd69937..45a65eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,6 @@ members = [ "d06t2", "d07t1", "d07t2", + "d08t1", + "d08t2", ] diff --git a/d08t1/Cargo.toml b/d08t1/Cargo.toml new file mode 100644 index 0000000..eb2e0ec --- /dev/null +++ b/d08t1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d08t1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/d08t1/demo_input.txt b/d08t1/demo_input.txt new file mode 100644 index 0000000..34ffa8a --- /dev/null +++ b/d08t1/demo_input.txt @@ -0,0 +1,5 @@ +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ) \ No newline at end of file diff --git a/d08t1/src/main.rs b/d08t1/src/main.rs new file mode 100644 index 0000000..5c4db7f --- /dev/null +++ b/d08t1/src/main.rs @@ -0,0 +1,40 @@ +use std::{collections::HashMap, fs::read_to_string}; + +fn main() { + let input = read_to_string("input.txt").unwrap(); + println!("d08t1: {}", d08t1(&input)); +} + +#[must_use] +pub fn d08t1(input: &str) -> usize { + let mut lines = input.lines(); + let mut directions = lines.next().unwrap().chars().cycle(); + let mut locations: HashMap = HashMap::new(); + for line in lines.skip(1) { + let (source, destination) = line.split_once(" = ").unwrap(); + let destination = &destination.replace(['(', ')'], ""); + let (left, right) = destination.split_once(", ").unwrap(); + locations.insert(source.to_string(), (left.to_string(), right.to_string())); + } + let mut result = 0; + let mut current_position = "AAA"; + while current_position != "ZZZ" { + result += 1; + current_position = if directions.next().unwrap() == 'L' { + &locations.get(current_position).unwrap().0 + } else { + &locations.get(current_position).unwrap().1 + }; + } + result +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d08t1() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d08t1(input), 6); + } +} diff --git a/d08t2/Cargo.toml b/d08t2/Cargo.toml new file mode 100644 index 0000000..1874d78 --- /dev/null +++ b/d08t2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d08t2" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/d08t2/demo_input.txt b/d08t2/demo_input.txt new file mode 100644 index 0000000..a8e2c98 --- /dev/null +++ b/d08t2/demo_input.txt @@ -0,0 +1,10 @@ +LR + +11A = (11B, XXX) +11B = (XXX, 11Z) +11Z = (11B, XXX) +22A = (22B, XXX) +22B = (22C, 22C) +22C = (22Z, 22Z) +22Z = (22B, 22B) +XXX = (XXX, XXX) \ No newline at end of file diff --git a/d08t2/src/main.rs b/d08t2/src/main.rs new file mode 100644 index 0000000..64606e6 --- /dev/null +++ b/d08t2/src/main.rs @@ -0,0 +1,65 @@ +use std::{collections::HashMap, fs::read_to_string}; + +fn main() { + let input = read_to_string("input.txt").unwrap(); + println!("d08t2: {}", d08t2(&input)); +} + +pub fn d08t2(input: &str) -> usize { + let mut lines = input.lines(); + let directions = lines.next().unwrap().chars().cycle(); + let mut locations: HashMap = HashMap::new(); + for line in lines.skip(1) { + let (source, destination) = line.split_once(" = ").unwrap(); + let destination = &destination.replace(['(', ')'], ""); + let (left, right) = destination.split_once(", ").unwrap(); + locations.insert(source.to_string(), (left.to_string(), right.to_string())); + } + let keys = locations.keys(); + keys.filter(|k| k.ends_with('A')) + .map(Clone::clone) + .map(|start| { + let mut dirs = directions.clone(); + let mut res = 0; + let mut current_position = start.clone(); + while !current_position.ends_with('Z') { + res += 1; + current_position = if dirs.next().unwrap() == 'L' { + &locations.get(¤t_position).unwrap().0 + } else { + &locations.get(¤t_position).unwrap().1 + } + .clone(); + } + res + }) + .reduce(lcm) + .unwrap() +} + +fn lcm(first: usize, second: usize) -> usize { + first * second / gcd(first, second) +} + +fn gcd(first: usize, second: usize) -> usize { + let mut max = first.max(second); + let mut min = first.min(second); + loop { + let res = max % min; + if res == 0 { + return min; + } + max = min; + min = res; + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d08t2() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d08t2(input), 6); + } +}