diff --git a/src/solutions/day04.rs b/src/solutions/day04.rs index efee037..1463597 100644 --- a/src/solutions/day04.rs +++ b/src/solutions/day04.rs @@ -10,10 +10,7 @@ impl Solution for Day04 { input: &mut Vec, ) -> Result, Box> { // Remove Card XXX: at start - let cards: Vec<&str> = input - .iter() - .map(|c| c.split(':').last().unwrap()) - .collect(); + let cards: Vec<&str> = input.iter().map(|c| c.split(':').last().unwrap()).collect(); let mut ans = 0; for card in cards { diff --git a/src/solutions/day08.rs b/src/solutions/day08.rs index e6fecfa..ee53d1b 100644 --- a/src/solutions/day08.rs +++ b/src/solutions/day08.rs @@ -74,10 +74,7 @@ impl Day08 { map.insert( key, ( - l.to_owned() - .replace([',', '('], "") - .trim() - .to_owned(), + l.to_owned().replace([',', '('], "").trim().to_owned(), r.trim().to_owned().replace(')', ""), ), ); diff --git a/src/solutions/day16.rs b/src/solutions/day16.rs index f54c3f9..5b89ccc 100644 --- a/src/solutions/day16.rs +++ b/src/solutions/day16.rs @@ -10,7 +10,9 @@ impl Solution for Day16 { ) -> Result, Box> { let grid = input.iter().map(|l| l.as_bytes()).collect::>(); - Ok(Box::new(self.traverse_grid(&grid, (0, 0, Direction::Right)))) + Ok(Box::new( + self.traverse_grid(&grid, (0, 0, Direction::Right)), + )) } fn part2( @@ -22,8 +24,17 @@ impl Solution for Day16 { // Itterate through every edge piece and record the max energy from a list of all of the combinations // O(n^2) :( Can't think of a better way of processing this // r: row, c: col - let ans = (0..grid.len()).flat_map(|r| [(r,0,Direction::Right), (r,grid[0].len()-1,Direction::Left)]) - .chain((0..grid[0].len()).flat_map(|c| [(0,c,Direction::Down), (grid.len()-1,c,Direction::Up)])) + let ans = (0..grid.len()) + .flat_map(|r| { + [ + (r, 0, Direction::Right), + (r, grid[0].len() - 1, Direction::Left), + ] + }) + .chain( + (0..grid[0].len()) + .flat_map(|c| [(0, c, Direction::Down), (grid.len() - 1, c, Direction::Up)]), + ) .map(|start| self.traverse_grid(&grid, start)) .max() .unwrap(); @@ -39,25 +50,35 @@ impl Solution for Day16 { impl Day16 { /// Returns the next coordinate after taking a step in the given direction fn step(&self, r: usize, c: usize, d: Direction) -> (usize, usize, Direction) { - let (dr, dc) = [(-1,0),(0,1),(1,0),(0,-1)][d as usize]; + let (dr, dc) = [(-1, 0), (0, 1), (1, 0), (0, -1)][d as usize]; ((r as isize + dr) as _, (c as isize + dc) as _, d) } - + /// Function which traverses through the grid and returns the number of tiles that are energised /// in the process - fn traverse_grid(&self, grid: &[&[u8]], start: (usize,usize, Direction)) -> usize { + fn traverse_grid(&self, grid: &[&[u8]], start: (usize, usize, Direction)) -> usize { let mut energised = vec![vec![[false; 4]; grid[0].len()]; grid.len()]; let mut beams = vec![start]; // Direction change given the current direction as an index - let right_lean = [Direction::Right, Direction::Up, Direction::Left, Direction::Down]; - let left_lean = [Direction::Left, Direction::Down, Direction::Right, Direction::Up]; + let right_lean = [ + Direction::Right, + Direction::Up, + Direction::Left, + Direction::Down, + ]; + let left_lean = [ + Direction::Left, + Direction::Down, + Direction::Right, + Direction::Up, + ]; // Trace all of the beams present in the list while !beams.is_empty() { let mut new_beams = Vec::with_capacity(beams.capacity()); // Row, col and direction - for (r,c,d) in beams { + for (r, c, d) in beams { if r >= grid.len() || c >= grid[0].len() { continue; } @@ -68,19 +89,29 @@ impl Day16 { // Trace the new path in a given direction and draw all of the new beams that come from it. match (grid[r][c], d) { - (b'/', _) => new_beams.push(self.step(r,c,right_lean[d as usize])), - (b'\\', _) => new_beams.push(self.step(r,c,left_lean[d as usize])), - (b'|', Direction::Left|Direction::Right) => new_beams.extend([self.step(r,c,Direction::Up), self.step(r,c,Direction::Down)]), - (b'-', Direction::Up|Direction::Down) => new_beams.extend([self.step(r,c,Direction::Left), self.step(r,c, Direction::Right)]), - _ => new_beams.push(self.step(r,c,d)), + (b'/', _) => new_beams.push(self.step(r, c, right_lean[d as usize])), + (b'\\', _) => new_beams.push(self.step(r, c, left_lean[d as usize])), + (b'|', Direction::Left | Direction::Right) => new_beams.extend([ + self.step(r, c, Direction::Up), + self.step(r, c, Direction::Down), + ]), + (b'-', Direction::Up | Direction::Down) => new_beams.extend([ + self.step(r, c, Direction::Left), + self.step(r, c, Direction::Right), + ]), + _ => new_beams.push(self.step(r, c, d)), } } beams = new_beams; } // Get the number of 'energised' tiles now that we have finished processing every beam - energised.iter().flatten().filter(|x| x.iter().any(|&b| b)).count() - } + energised + .iter() + .flatten() + .filter(|x| x.iter().any(|&b| b)) + .count() + } } /// Test from puzzle input @@ -122,4 +153,4 @@ mod test { assert_eq!(answer, "51"); } -} \ No newline at end of file +} diff --git a/src/solutions/day17.rs b/src/solutions/day17.rs index 03d8705..c1fde42 100644 --- a/src/solutions/day17.rs +++ b/src/solutions/day17.rs @@ -1,6 +1,6 @@ use super::Solution; use crate::utils::Direction; -use std::collections::{HashMap, BinaryHeap}; +use std::collections::{BinaryHeap, HashMap}; #[derive(Debug, Copy, Clone, Eq, PartialEq)] struct State { @@ -56,8 +56,18 @@ impl Solution for Day17 { grid.insert((x, y), col.to_digit(10).unwrap()); } } - - Ok(Box::new(self.dijkstra((0, 0), (input[0].len() - 1, input.len() - 1), (input[0].len(), input.len()), 1, 3, grid).unwrap())) + + Ok(Box::new( + self.dijkstra( + (0, 0), + (input[0].len() - 1, input.len() - 1), + (input[0].len(), input.len()), + 1, + 3, + grid, + ) + .unwrap(), + )) } fn part2( @@ -71,8 +81,18 @@ impl Solution for Day17 { grid.insert((x, y), col.to_digit(10).unwrap()); } } - - Ok(Box::new(self.dijkstra((0, 0), (input[0].len() - 1, input.len() - 1), (input[0].len(), input.len()), 4, 10, grid).unwrap())) + + Ok(Box::new( + self.dijkstra( + (0, 0), + (input[0].len() - 1, input.len() - 1), + (input[0].len(), input.len()), + 4, + 10, + grid, + ) + .unwrap(), + )) } fn get_day(&self) -> u8 { @@ -82,7 +102,7 @@ impl Solution for Day17 { impl Day17 { fn dijkstra( - &self, + &self, start: (usize, usize), end: (usize, usize), lens: (usize, usize), @@ -108,7 +128,7 @@ impl Day17 { dist.insert(st2.into(), 0); heap.push(st1); heap.push(st2); - + while let Some( state @ State { cost, @@ -131,9 +151,10 @@ impl Day17 { dir: d, steps: if d == dir { steps + 1 } else { 1 }, }; - if next.steps > max_step || dist.get(&next.into()).is_some_and(|&c| c <= next.cost) { + if next.steps > max_step || dist.get(&next.into()).is_some_and(|&c| c <= next.cost) + { // For p1 and p2 - // Streak gets too long + // Streak gets too long // Or a better solution/path already exists continue; } @@ -148,8 +169,13 @@ impl Day17 { } None } - - fn neighbours(&self, (x, y): (usize, usize), dir: Direction, (x_len, y_len): (usize, usize)) -> Vec<((usize, usize), Direction)> { + + fn neighbours( + &self, + (x, y): (usize, usize), + dir: Direction, + (x_len, y_len): (usize, usize), + ) -> Vec<((usize, usize), Direction)> { const fn deltas(x: usize, y: usize) -> [((usize, usize), Direction); 4] { [ ((x, y.saturating_sub(1)), Direction::Up), @@ -158,7 +184,7 @@ impl Day17 { ((x + 1, y), Direction::Right), ] } - + deltas(x, y) .into_iter() .filter_map(|(n, d)| { @@ -211,4 +237,4 @@ mod test { assert_eq!(answer, "94"); } -} \ No newline at end of file +} diff --git a/src/solutions/day18.rs b/src/solutions/day18.rs index c71126c..f5a7b46 100644 --- a/src/solutions/day18.rs +++ b/src/solutions/day18.rs @@ -63,4 +63,4 @@ mod test { assert_eq!(answer, "Ready"); } -} \ No newline at end of file +} diff --git a/src/solutions/day19.rs b/src/solutions/day19.rs index 82771cb..e47c36a 100644 --- a/src/solutions/day19.rs +++ b/src/solutions/day19.rs @@ -63,4 +63,4 @@ mod test { assert_eq!(answer, "Ready"); } -} \ No newline at end of file +} diff --git a/src/solutions/day20.rs b/src/solutions/day20.rs index 90e4d2e..ce72d5b 100644 --- a/src/solutions/day20.rs +++ b/src/solutions/day20.rs @@ -63,4 +63,4 @@ mod test { assert_eq!(answer, "Ready"); } -} \ No newline at end of file +} diff --git a/src/solutions/day21.rs b/src/solutions/day21.rs index f52e993..7491fb8 100644 --- a/src/solutions/day21.rs +++ b/src/solutions/day21.rs @@ -63,4 +63,4 @@ mod test { assert_eq!(answer, "Ready"); } -} \ No newline at end of file +} diff --git a/src/solutions/day22.rs b/src/solutions/day22.rs index 99adc58..e784924 100644 --- a/src/solutions/day22.rs +++ b/src/solutions/day22.rs @@ -63,4 +63,4 @@ mod test { assert_eq!(answer, "Ready"); } -} \ No newline at end of file +} diff --git a/src/solutions/day23.rs b/src/solutions/day23.rs index 09353ab..e38c818 100644 --- a/src/solutions/day23.rs +++ b/src/solutions/day23.rs @@ -63,4 +63,4 @@ mod test { assert_eq!(answer, "Ready"); } -} \ No newline at end of file +} diff --git a/src/solutions/day24.rs b/src/solutions/day24.rs index 671b699..920f2e2 100644 --- a/src/solutions/day24.rs +++ b/src/solutions/day24.rs @@ -63,4 +63,4 @@ mod test { assert_eq!(answer, "Ready"); } -} \ No newline at end of file +} diff --git a/src/solutions/day25.rs b/src/solutions/day25.rs index 9923219..05193fe 100644 --- a/src/solutions/day25.rs +++ b/src/solutions/day25.rs @@ -63,4 +63,4 @@ mod test { assert_eq!(answer, "Ready"); } -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index 019c19a..aaf92e3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -61,7 +61,7 @@ pub enum Direction { Up, Right, Down, - Left + Left, } impl Direction { @@ -71,7 +71,7 @@ impl Direction { Direction::Up => Direction::Down, Direction::Down => Direction::Up, Direction::Left => Direction::Right, - Direction::Right => Direction::Left + Direction::Right => Direction::Left, } } -} \ No newline at end of file +}