From 4a095e2a74f2addb1a408d105049202a8489a55b Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 8 Dec 2023 13:50:47 +0000 Subject: [PATCH] Attempted part 2 but have made a mistake in how I simulatneously traverse them --- src/input/day08_test2 | 17 ++++---- src/solutions/day08.rs | 90 +++++++++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 35 deletions(-) diff --git a/src/input/day08_test2 b/src/input/day08_test2 index 59e2d47..a8e2c98 100644 --- a/src/input/day08_test2 +++ b/src/input/day08_test2 @@ -1,9 +1,10 @@ -RL +LR -AAA = (BBB, CCC) -BBB = (DDD, EEE) -CCC = (ZZZ, GGG) -DDD = (DDD, DDD) -EEE = (EEE, EEE) -GGG = (GGG, GGG) -ZZZ = (ZZZ, ZZZ) \ No newline at end of file +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/src/solutions/day08.rs b/src/solutions/day08.rs index 080b34d..5bc5c4b 100644 --- a/src/solutions/day08.rs +++ b/src/solutions/day08.rs @@ -14,37 +14,38 @@ impl Solution for Day08 { self.populate_map(&input[2..input.len()], &mut map)?; - let mut count = 0u32; - let mut current = map.get("AAA").unwrap(); - let mut found = false; - - while !found { - for i in instructions.chars() { - count += 1; - let next: &String; - match i { - 'L' => next = ¤t.0, - 'R' => next = ¤t.1, - _ => { - continue; - } - } - if next == "ZZZ" { - found = true; - break; - } - current = map.get(next).unwrap(); - } - } - - Ok(Box::new(count)) + Ok(Box::new(self.traverse_to_end( + &"AAA".to_owned(), + &"ZZZ".to_owned(), + &instructions, + &map, + )?)) } fn part2( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let instructions: String = input[0].clone(); + + let mut map: HashMap = HashMap::new(); + self.populate_map(&input[2..input.len()], &mut map)?; + + let mut start_points = vec![]; + for route in &input[2..input.len()] { + if route.as_bytes()[2] as char == 'A' { + start_points.push(route.split_at(3).0.to_owned()); + } + } + + let mut count = 0u32; + + println!("{:?}", start_points); + for start in start_points { + count += self.traverse_to_end(&start, &"Z".to_owned(), &instructions, &map)? + } + + Ok(Box::new(count)) } fn get_day(&self) -> u8 { @@ -53,6 +54,8 @@ impl Solution for Day08 { } impl Day08 { + /// Given a string in the format XXX = (XXX, XXX), will populate a map witht he Xs + /// To create a way of traversing the route fn populate_map<'a>( &self, input: &[String], @@ -78,6 +81,39 @@ impl Day08 { Ok(()) } + + /// Function to traverse the map going left and right based on given instructions + fn traverse_to_end( + &self, + start: &String, + end: &String, + instructions: &String, + map: &HashMap, + ) -> Result> { + let mut count = 0u32; + let mut current = map.get(start).unwrap(); + let mut found = false; + + while !found { + for i in instructions.chars() { + count += 1; + let next: &String; + match i { + 'L' => next = ¤t.0, + 'R' => next = ¤t.1, + _ => { + continue; + } + } + if next.ends_with("ZZZ") { + found = true; + break; + } + current = map.get(next).unwrap(); + } + } + Ok(count) + } } /// Test from puzzle input @@ -117,6 +153,6 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "6"); } }