From 5c46e12269e6a427a149bd679ff0913d2781aba3 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 11 Dec 2023 11:30:54 +0000 Subject: [PATCH] Day 10 Part 2 Complete --- src/input/day10_test2 | 15 ++++--- src/solutions/day10.rs | 89 +++++++++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 32 deletions(-) diff --git a/src/input/day10_test2 b/src/input/day10_test2 index 3c00cf2..2e5dcbb 100644 --- a/src/input/day10_test2 +++ b/src/input/day10_test2 @@ -1,5 +1,10 @@ -..F7. -.FJ|. -SJ.L7 -|F--J -LJ... \ No newline at end of file +.F----7F7F7F7F-7.... +.|F--7||||||||FJ.... +.||.FJ||||||||L7.... +FJL7L7LJLJ||LJ.L-7.. +L--J.L7...LJS7F-7L7. +....F-J..F7FJ|L7L7L7 +....L7.F7||L7|.L7L7| +.....|FJLJ|FJ|F7|.LJ +....FJL-7.||.||||... +....L---J.LJ.LJLJ... \ No newline at end of file diff --git a/src/solutions/day10.rs b/src/solutions/day10.rs index f30ffd2..20756b2 100644 --- a/src/solutions/day10.rs +++ b/src/solutions/day10.rs @@ -9,37 +9,31 @@ impl Solution for Day10 { &self, input: &mut Vec, ) -> Result, Box> { - let mut start = (0, 0); - let mut graph = input - .iter() - .enumerate() - .map(|(r, line)| { - line.bytes() - .enumerate() - .map(|(c, tile)| { - if tile == b'S' { - start = (r, c); - } - self.connections(tile) - }) - .collect::>() - }) - .collect::>(); - let pipe_loop = "J|-L7F" - .bytes() - .find_map(|start_tile| { - graph[start.0][start.1] = self.connections(start_tile); - self.find_loop(&graph, start) - }) - .unwrap(); + let (start, mut graph) = self.generate_graph(input); + let pipe_loop = self.generate_loop(start, &mut graph)?; Ok(Box::new(pipe_loop.len() / 2)) } fn part2( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let mut ans = 0usize; + + let (start, mut graph) = self.generate_graph(input); + let pipe_loop = self.generate_loop(start, &mut graph)?; + + for r in 0..graph.len() { + let mut inside = false; + for c in 0..graph[0].len() { + if !pipe_loop.contains(&(r, c)) { + ans += inside as usize; + } else if graph[r][c][0] { + inside = !inside; + } + } + } + Ok(Box::new(ans)) } fn get_day(&self) -> u8 { @@ -48,6 +42,8 @@ impl Solution for Day10 { } impl Day10 { + /// Traces the graph and populates a hashset with + /// loops present within the connections fn find_loop( &self, graph: &[Vec<[bool; 4]>], @@ -86,6 +82,7 @@ impl Day10 { } } + /// Retruns all of the connections around a given tile fn connections(&self, tile: u8) -> [bool; 4] { match tile { // [ up, right, down, left] @@ -98,6 +95,44 @@ impl Day10 { _ => [false, false, false, false], } } + + /// Fucntion to generate a graph of all of the connections of the the nodes + fn generate_graph(&self, input: &mut Vec) -> ((usize, usize), Vec>) { + let mut start = (0, 0); + let graph = input + .iter() + .enumerate() + .map(|(r, line)| { + line.bytes() + .enumerate() + .map(|(c, tile)| { + if tile == b'S' { + start = (r, c); + } + self.connections(tile) + }) + .collect::>() + }) + .collect::>(); + + (start, graph) + } + + /// Function that generates the a hashset of the loops in the graph + fn generate_loop( + &self, + start: (usize, usize), + graph: &mut [Vec<[bool; 4]>], + ) -> Result, Box> { + let pipe_loop = "J|-L7F" + .bytes() + .find_map(|start_tile| { + graph[start.0][start.1] = self.connections(start_tile); + self.find_loop(&graph, start) + }) + .unwrap(); + Ok(pipe_loop) + } } /// Test from puzzle input @@ -120,7 +155,7 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "8"); } #[test] @@ -137,6 +172,6 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "8"); } }