Day 10 Part 2 Complete

This commit is contained in:
Luke Else 2023-12-11 11:30:54 +00:00
parent 075f7e39c7
commit 5c46e12269
2 changed files with 72 additions and 32 deletions

View File

@ -1,5 +1,10 @@
..F7. .F----7F7F7F7F-7....
.FJ|. .|F--7||||||||FJ....
SJ.L7 .||.FJ||||||||L7....
|F--J FJL7L7LJLJ||LJ.L-7..
LJ... L--J.L7...LJS7F-7L7.
....F-J..F7FJ|L7L7L7
....L7.F7||L7|.L7L7|
.....|FJLJ|FJ|F7|.LJ
....FJL-7.||.||||...
....L---J.LJ.LJLJ...

View File

@ -9,37 +9,31 @@ impl Solution for Day10 {
&self, &self,
input: &mut Vec<String>, input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> { ) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
let mut start = (0, 0); let (start, mut graph) = self.generate_graph(input);
let mut graph = input let pipe_loop = self.generate_loop(start, &mut graph)?;
.iter()
.enumerate()
.map(|(r, line)| {
line.bytes()
.enumerate()
.map(|(c, tile)| {
if tile == b'S' {
start = (r, c);
}
self.connections(tile)
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
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(Box::new(pipe_loop.len() / 2)) Ok(Box::new(pipe_loop.len() / 2))
} }
fn part2( fn part2(
&self, &self,
_input: &mut Vec<String>, input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> { ) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
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 { fn get_day(&self) -> u8 {
@ -48,6 +42,8 @@ impl Solution for Day10 {
} }
impl Day10 { impl Day10 {
/// Traces the graph and populates a hashset with
/// loops present within the connections
fn find_loop( fn find_loop(
&self, &self,
graph: &[Vec<[bool; 4]>], 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] { fn connections(&self, tile: u8) -> [bool; 4] {
match tile { match tile {
// [ up, right, down, left] // [ up, right, down, left]
@ -98,6 +95,44 @@ impl Day10 {
_ => [false, false, false, false], _ => [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<String>) -> ((usize, usize), Vec<Vec<[bool; 4]>>) {
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::<Vec<_>>()
})
.collect::<Vec<_>>();
(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<HashSet<(usize, usize)>, Box<dyn std::error::Error>> {
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 /// Test from puzzle input
@ -120,7 +155,7 @@ mod test {
.unwrap() .unwrap()
.to_string(); .to_string();
assert_eq!(answer, "Ready"); assert_eq!(answer, "8");
} }
#[test] #[test]
@ -137,6 +172,6 @@ mod test {
.unwrap() .unwrap()
.to_string(); .to_string();
assert_eq!(answer, "Ready"); assert_eq!(answer, "8");
} }
} }