From cfec49711205049cedb070a55ec21416d29b6d5f Mon Sep 17 00:00:00 2001 From: Luke Else Date: Sun, 17 Dec 2023 19:15:40 +0000 Subject: [PATCH] Day 16 Part 1 Complete -> Made it nice and easy for day 2 :) --- src/SOLUTUION | 3 -- src/input/day16 | 110 +++++++++++++++++++++++++++++++++++++++++ src/input/day16_test1 | 10 ++++ src/input/day16_test2 | 10 ++++ src/main.rs | 2 +- src/solutions/day16.rs | 51 +++++++++++++++++-- 6 files changed, 177 insertions(+), 9 deletions(-) delete mode 100644 src/SOLUTUION diff --git a/src/SOLUTUION b/src/SOLUTUION deleted file mode 100644 index 7ad19b2..0000000 --- a/src/SOLUTUION +++ /dev/null @@ -1,3 +0,0 @@ -This one was super fun - I really enjoyed it. I defined two enumerated types: one ordered lowest to highest for the card ranks, and another ranked lowest to highest for the hand ranks. The real meat of the code is in function that evaluates the rank of a hand: I used a hash map to build a histogram of each hand, then used the card counts to determine what kind of hand I had for Part 1. I then fed this into a comparison function that compared the ranking of two hands to determine less, equal, or greater, and then fed that to the standard sort function to rank the entire vector of hands and their associated bids. It was then trivial to iterate over the sorted vector to multiply the ranking of each hand with its bid and add it to the sum. - -Part 2 was slightly harder, just reworked the card ranking to make the wildcard the lowest ranked card, then reworked the logic in the hand ranking function to take into account the presence of any wild cards in the hand. \ No newline at end of file diff --git a/src/input/day16 b/src/input/day16 index e69de29..c629e96 100644 --- a/src/input/day16 +++ b/src/input/day16 @@ -0,0 +1,110 @@ +\...\............................../...\....-..../...|....../.......-...........................|............. +.............|/.../............................-....|......../...........|.|................/\-..|....\...../. +....\......-.....||-\........................|/........|............/.......................|\...|............ +|.....-....\.|......-.\....\-......|.........../|......../\...\.-..........-.........\...-..\........./....... +............../.|..........|.......|.......\.\...............|../......................\...................... +.-.-...|....-.-.......\...\..../....../.......-..................\......./....................-........-...... +..........\.....|.....\.....\..................................................................|.............. +...../..................../...........-..-.........................-...................................../-... +......\......\.............|..../..........\.........................\..\../....../...........-...........-... +.....|./..........-....\....|........./..........-..............-./........../..............-...\...../....... +...../...../.......-..............|......\\../......../........../............../......\......\...../........- +..\................|......\.............\|............./..../...-...........................|-../|...|......./ +....|.........-.|....-....-.....|.......................................-...../.\.............\.........-..... +.........-........././................\../...........\.......\......./.....\................\...\............. +.............|........\....|........\........................././\............-............./............/.... +................./...-..........|..............--../.|\............./......................................... +.|../.....|................|........../.......-.......-.............\......................................... +.............\..................\......../.......\/................\....\......../...././....................| +.................\................................../..........................................|.............. +................|..................../..................\...........................|./..........-.........-.. +...|.............\.....-..||...-......../.........................../......................-..-|.........-.... +.\...........................-../................|.../............/....-......\............................../ +.........................|...............|...........|.|.............\....\...../........-.--/................ +./...|.........-.............|..-../..........\........./../...........................|.....................\ +.|.....-......\..-..................\..|.../................................/\......-.........../...../..../.. +...\...............\....\../.........|\................-......................................\............... +....../..-|...................................\.........|...-....|..../.......................|/.............. +.........|.|..............-...................../.................|./-....|../..........\..............|...... +.......\..|...\...........|....................|.-......................\..-.../.......\|..........\.../.|.\.| +-.............................|....................|.....|.........-.........|..-........./.......|\.|.../.... +.-.......\...................\.....\../......|..............|...................-............./.....-......... +...|..................../..|\....\..-............\................\..................................|....-... +...............\....................\.|\....-.............|....../.................\..........\....../.....\.. +......|..\......................-........\.......\.../.|......-...............|......./\../........../........ +.........-..../........-\.........-.......-.....|.|....\.\........\.-.......|.........-....|...--.-..|........ +.....|..|.|....-./.....|.....|...........|/\...../.............\/....|-........./..-.................../...... +.|....../..............-...-............-.......-....................\..-...-.|............................... +|.............|............................|.............................-.................................... +....../....-../..........\...|......-.|...........\..|..................-...../../-..\.........\.....|........ +......\../..................-..............-.../.......-.............................\..-....\/............... +............\......................./..................|....-....................-........../.....-.......-... +...............|...........-./.......\...-..../....|....-........./..-.........-...........-.\................ +/..................../.................\.....-................./\/........../............./..|................ +....................../.............../\..........|.....\|../...-..\............|........./................... +......................-../.|...............................................-......./..\......|................ +.......\...............|..............|.../............................\........./....|.....|................| +....\......../....-...|.................-..........-...|/./...................../.\........................... +.........|......./.............|../..-.......................-|...........\.........|...|..................... +..\............../.......................-....\.................-.....|.........-............................. +.....-/...............|......|..../......-...|..../...........-................................-.....\...\...\ +......................../.......\................./..\..........\............|...................\............ +-....................../...........\......./...|...........\|./........-..................\...-......../...... +.......|./..........|...\................/..............|.\.............\.\..................................\ +.|............\.......\..........\./..........-...................../..|............................../....... +........................../....../...|......-..\..............|-.|....\-...|../.........\......-.\./.......... +....../.........../.........../..\......./......../-...\...........\............/.....................|...\... +...-....../.../.....\....-...........\..................../.........-......................\.................. +/............|../.......................-.........|....../......|.......|............/-..../..../............| +../..|...............|../-................/.........................................|......................... +.....-..........-\-...........|....../\....................|......................-.....-..........\.......... +.-.....\......./.....\............../...|...|............-...|...../.............\...\../|...../...|......../. +.../......-..|...................\-...../..-..|.......|..-..../........|....-.../.......|........|......|..... +.-....../............/........-/..........\........\...........................\..........................|... +.....|...|........................................../...........................-|.......|..-................. +|...........|.\..............|......................-|./.......|../....\-.|\.../...-.......|.......|.......... +.|.........\.|../.....-.........\......\..\...........|...................\.........../......|.......-..../... +...............|.....................|......|.................../.............|.....-..|..........-........... +|.............|...........|............../......................\..........-.............|.....-.............. +.\............-../\.................-.......|.-............../........\.......|.............\.....\........... +.../.\.....\|..-.............\.....................................|.................-............/..........| +.../../.............................-...|.......|..............-............................-................. +......................\-\............-......|.-............|.\...............-.../......\.........-........... +............................|..\.-.............../.\.../.......-..................\........................... +..../..\........-........\......../..\......../......\./..-|....|........\............|...................|\.. +..................................................|...\..../........................................-......... +...\......-............\.......-...\...\............|\..../\....../.||........................................ +...............-................-......-....../.../............................-.|....|................./..\.. +......././../....-...................................................\..............................||........ +|/.....\........./....................\......-.-.....|......|................../............................\. +................./..\...................\......\.........\..............-.....-....................\..-..../.\ +...\......|..-.........-.....|.........-........|.............|..............\............-................... +.\..........|.............-............../../......../........................................................ +....\....................................................-......../..-........|.....\..........\.............. +.......-..\....................-..|.....-..../.-......../............\.............|..............|../........ +...|/....-.............-......../\...........|................/..................\.....|....................-. +.....-................./.|......../..........-......|...\.....-..............-...-..|.......................|. +.........\..../...........-........|--......-.........-.........\...-.......|.\........-.........|.........../ +........-...|../......../........\.....|.....|.....\................/.......-.....-.........../..|........\... +.-....\................||.......|...\..\......./.....-............|../...............|........./......-...-... +.......\......\............../.|.......................................|.......................|........./|... +............|../........-............................/.........-.........-..\..\..../.../.\...\.\............. +....................\...|.....\/...|\.|.................\.|...............................................\-.. +.................\|...-........./\.......|.......................................-.........\.................. +.......-.......|.............\....|...\.....-.\./.....-..................\\........-..\......-....../......... +............-...../....................|...-....../..............-..../................|...........-.......... +............../\..........|........|./\\..|.....|../....-......../..............-............\......./...\.... +............./....../......-...............\|..\............../......\.......................-.......|..-..... +.....-........./....\......................\...../...|./..\.../\...../.......\...............\............../. +...................................|..../../.-....................-.....-.......-...................-....-.... +..................................................-......|-............|-...\.\...............\............... +.............../...........\................/.....\...........-.....|......................-............-..... +.............|.....|......................./.......|.............................\.|..........\../........\... +........................|..............\............................/...................-..|.......\.\........ +.......|....../............/...............-..........|\.........|.......\.....|.............\/../...-...-.... +......\.\.....-..||...-.\........\....|...../\/....\........../........|......|............................... +..\........-../....\/.....................-...........................|....................................... +..............|../.|...............\...\.........|..\............../......./.-.....\......-..../.............. +....|....-............../......-.\........-............|.|..|.........................../.......|............. +........\............./...................|......../....|..-.....................|./.......\.................. +............./.|...........\.............................../.....\....|/.......|.|........./.........|........ \ No newline at end of file diff --git a/src/input/day16_test1 b/src/input/day16_test1 index e69de29..c78b2e7 100644 --- a/src/input/day16_test1 +++ b/src/input/day16_test1 @@ -0,0 +1,10 @@ +.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|.... \ No newline at end of file diff --git a/src/input/day16_test2 b/src/input/day16_test2 index e69de29..c78b2e7 100644 --- a/src/input/day16_test2 +++ b/src/input/day16_test2 @@ -0,0 +1,10 @@ +.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|.... \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4fa7046..9fce061 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ async fn main() -> Result<(), Box> { Box::new(day13::Day13 {}), Box::new(day14::Day14 {}), Box::new(day15::Day15 {}), - // Box::new(day16::Day16 {}), + Box::new(day16::Day16 {}), // Box::new(day17::Day17 {}), // Box::new(day18::Day18 {}), // Box::new(day19::Day19 {}), diff --git a/src/solutions/day16.rs b/src/solutions/day16.rs index e7ef67c..a91d730 100644 --- a/src/solutions/day16.rs +++ b/src/solutions/day16.rs @@ -5,9 +5,11 @@ pub struct Day16 {} impl Solution for Day16 { fn part1( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let grid = input.iter().map(|l| l.as_bytes()).collect::>(); + + Ok(Box::new(self.traverse_grid(&grid, (0, 0, 1)))) } fn part2( @@ -22,7 +24,46 @@ impl Solution for Day16 { } } -impl Day16 {} +impl Day16 { + /// Returns the next coordinate after taking a step in the given direction + fn step(&self, r: usize, c: usize, d: usize) -> (usize, usize, usize) { + let (dr, dc) = [(-1,0),(0,1),(1,0),(0,-1)][d]; + ((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,usize)) -> usize { + let mut seen = vec![vec![[false; 4]; grid[0].len()]; grid.len()]; + let mut beams = vec![start]; + + // 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 { + if r >= grid.len() || c >= grid[0].len() { + continue; + } + if seen[r][c][d] { + continue; + } + seen[r][c][d] = true; + + // 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,[1,0,3,2][d])), + (b'\\', _) => new_beams.push(self.step(r,c,[3,2,1,0][d])), + (b'|', 1|3) => new_beams.extend([self.step(r,c,0), self.step(r,c,2)]), + (b'-', 0|2) => new_beams.extend([self.step(r,c,1), self.step(r,c,3)]), + _ => new_beams.push(self.step(r,c,d)), + } + } + beams = new_beams; + } + seen.iter().flat_map(|row| row).filter(|x| x.iter().any(|&b| b)).count() + } +} /// Test from puzzle input #[cfg(test)] @@ -44,7 +85,7 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "46"); } #[test] @@ -61,6 +102,6 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "51"); } } \ No newline at end of file