Day 16 Part 1 Complete -> Made it nice and easy for day 2 :)

This commit is contained in:
Luke Else 2023-12-17 19:15:40 +00:00
parent 0bead087c3
commit cfec497112
6 changed files with 177 additions and 9 deletions

View File

@ -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.

View File

@ -0,0 +1,110 @@
\...\............................../...\....-..../...|....../.......-...........................|.............
.............|/.../............................-....|......../...........|.|................/\-..|....\...../.
....\......-.....||-\........................|/........|............/.......................|\...|............
|.....-....\.|......-.\....\-......|.........../|......../\...\.-..........-.........\...-..\........./.......
............../.|..........|.......|.......\.\...............|../......................\......................
.-.-...|....-.-.......\...\..../....../.......-..................\......./....................-........-......
..........\.....|.....\.....\..................................................................|..............
...../..................../...........-..-.........................-...................................../-...
......\......\.............|..../..........\.........................\..\../....../...........-...........-...
.....|./..........-....\....|........./..........-..............-./........../..............-...\...../.......
...../...../.......-..............|......\\../......../........../............../......\......\...../........-
..\................|......\.............\|............./..../...-...........................|-../|...|......./
....|.........-.|....-....-.....|.......................................-...../.\.............\.........-.....
.........-........././................\../...........\.......\......./.....\................\...\.............
.............|........\....|........\........................././\............-............./............/....
................./...-..........|..............--../.|\............./.........................................
.|../.....|................|........../.......-.......-.............\.........................................
.............\..................\......../.......\/................\....\......../...././....................|
.................\................................../..........................................|..............
................|..................../..................\...........................|./..........-.........-..
...|.............\.....-..||...-......../.........................../......................-..-|.........-....
.\...........................-../................|.../............/....-......\............................../
.........................|...............|...........|.|.............\....\...../........-.--/................
./...|.........-.............|..-../..........\........./../...........................|.....................\
.|.....-......\..-..................\..|.../................................/\......-.........../...../..../..
...\...............\....\../.........|\................-......................................\...............
....../..-|...................................\.........|...-....|..../.......................|/..............
.........|.|..............-...................../.................|./-....|../..........\..............|......
.......\..|...\...........|....................|.-......................\..-.../.......\|..........\.../.|.\.|
-.............................|....................|.....|.........-.........|..-........./.......|\.|.../....
.-.......\...................\.....\../......|..............|...................-............./.....-.........
...|..................../..|\....\..-............\................\..................................|....-...
...............\....................\.|\....-.............|....../.................\..........\....../.....\..
......|..\......................-........\.......\.../.|......-...............|......./\../........../........
.........-..../........-\.........-.......-.....|.|....\.\........\.-.......|.........-....|...--.-..|........
.....|..|.|....-./.....|.....|...........|/\...../.............\/....|-........./..-.................../......
.|....../..............-...-............-.......-....................\..-...-.|...............................
|.............|............................|.............................-....................................
....../....-../..........\...|......-.|...........\..|..................-...../../-..\.........\.....|........
......\../..................-..............-.../.......-.............................\..-....\/...............
............\......................./..................|....-....................-........../.....-.......-...
...............|...........-./.......\...-..../....|....-........./..-.........-...........-.\................
/..................../.................\.....-................./\/........../............./..|................
....................../.............../\..........|.....\|../...-..\............|........./...................
......................-../.|...............................................-......./..\......|................
.......\...............|..............|.../............................\........./....|.....|................|
....\......../....-...|.................-..........-...|/./...................../.\...........................
.........|......./.............|../..-.......................-|...........\.........|...|.....................
..\............../.......................-....\.................-.....|.........-.............................
.....-/...............|......|..../......-...|..../...........-................................-.....\...\...\
......................../.......\................./..\..........\............|...................\............
-....................../...........\......./...|...........\|./........-..................\...-......../......
.......|./..........|...\................/..............|.\.............\.\..................................\
.|............\.......\..........\./..........-...................../..|............................../.......
........................../....../...|......-..\..............|-.|....\-...|../.........\......-.\./..........
....../.........../.........../..\......./......../-...\...........\............/.....................|...\...
...-....../.../.....\....-...........\..................../.........-......................\..................
/............|../.......................-.........|....../......|.......|............/-..../..../............|
../..|...............|../-................/.........................................|.........................
.....-..........-\-...........|....../\....................|......................-.....-..........\..........
.-.....\......./.....\............../...|...|............-...|...../.............\...\../|...../...|......../.
.../......-..|...................\-...../..-..|.......|..-..../........|....-.../.......|........|......|.....
.-....../............/........-/..........\........\...........................\..........................|...
.....|...|........................................../...........................-|.......|..-.................
|...........|.\..............|......................-|./.......|../....\-.|\.../...-.......|.......|..........
.|.........\.|../.....-.........\......\..\...........|...................\.........../......|.......-..../...
...............|.....................|......|.................../.............|.....-..|..........-...........
|.............|...........|............../......................\..........-.............|.....-..............
.\............-../\.................-.......|.-............../........\.......|.............\.....\...........
.../.\.....\|..-.............\.....................................|.................-............/..........|
.../../.............................-...|.......|..............-............................-.................
......................\-\............-......|.-............|.\...............-.../......\.........-...........
............................|..\.-.............../.\.../.......-..................\...........................
..../..\........-........\......../..\......../......\./..-|....|........\............|...................|\..
..................................................|...\..../........................................-.........
...\......-............\.......-...\...\............|\..../\....../.||........................................
...............-................-......-....../.../............................-.|....|................./..\..
......././../....-...................................................\..............................||........
|/.....\........./....................\......-.-.....|......|................../............................\.
................./..\...................\......\.........\..............-.....-....................\..-..../.\
...\......|..-.........-.....|.........-........|.............|..............\............-...................
.\..........|.............-............../../......../........................................................
....\....................................................-......../..-........|.....\..........\..............
.......-..\....................-..|.....-..../.-......../............\.............|..............|../........
...|/....-.............-......../\...........|................/..................\.....|....................-.
.....-................./.|......../..........-......|...\.....-..............-...-..|.......................|.
.........\..../...........-........|--......-.........-.........\...-.......|.\........-.........|.........../
........-...|../......../........\.....|.....|.....\................/.......-.....-.........../..|........\...
.-....\................||.......|...\..\......./.....-............|../...............|........./......-...-...
.......\......\............../.|.......................................|.......................|........./|...
............|../........-............................/.........-.........-..\..\..../.../.\...\.\.............
....................\...|.....\/...|\.|.................\.|...............................................\-..
.................\|...-........./\.......|.......................................-.........\..................
.......-.......|.............\....|...\.....-.\./.....-..................\\........-..\......-....../.........
............-...../....................|...-....../..............-..../................|...........-..........
............../\..........|........|./\\..|.....|../....-......../..............-............\......./...\....
............./....../......-...............\|..\............../......\.......................-.......|..-.....
.....-........./....\......................\...../...|./..\.../\...../.......\...............\............../.
...................................|..../../.-....................-.....-.......-...................-....-....
..................................................-......|-............|-...\.\...............\...............
.............../...........\................/.....\...........-.....|......................-............-.....
.............|.....|......................./.......|.............................\.|..........\../........\...
........................|..............\............................/...................-..|.......\.\........
.......|....../............/...............-..........|\.........|.......\.....|.............\/../...-...-....
......\.\.....-..||...-.\........\....|...../\/....\........../........|......|...............................
..\........-../....\/.....................-...........................|.......................................
..............|../.|...............\...\.........|..\............../......./.-.....\......-..../..............
....|....-............../......-.\........-............|.|..|.........................../.......|.............
........\............./...................|......../....|..-.....................|./.......\..................
............./.|...........\.............................../.....\....|/.......|.|........./.........|........

View File

@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

View File

@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

View File

@ -23,7 +23,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Box::new(day13::Day13 {}), Box::new(day13::Day13 {}),
Box::new(day14::Day14 {}), Box::new(day14::Day14 {}),
Box::new(day15::Day15 {}), Box::new(day15::Day15 {}),
// Box::new(day16::Day16 {}), Box::new(day16::Day16 {}),
// Box::new(day17::Day17 {}), // Box::new(day17::Day17 {}),
// Box::new(day18::Day18 {}), // Box::new(day18::Day18 {}),
// Box::new(day19::Day19 {}), // Box::new(day19::Day19 {}),

View File

@ -5,9 +5,11 @@ pub struct Day16 {}
impl Solution for Day16 { impl Solution for Day16 {
fn part1( fn part1(
&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 grid = input.iter().map(|l| l.as_bytes()).collect::<Vec<_>>();
Ok(Box::new(self.traverse_grid(&grid, (0, 0, 1))))
} }
fn part2( 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 /// Test from puzzle input
#[cfg(test)] #[cfg(test)]
@ -44,7 +85,7 @@ mod test {
.unwrap() .unwrap()
.to_string(); .to_string();
assert_eq!(answer, "Ready"); assert_eq!(answer, "46");
} }
#[test] #[test]
@ -61,6 +102,6 @@ mod test {
.unwrap() .unwrap()
.to_string(); .to_string();
assert_eq!(answer, "Ready"); assert_eq!(answer, "51");
} }
} }