Day 13 Complete 0 -> Initially implemented with a hashmap however this was not viable for the second part so followed AxlLinds solution to adapt it for the second part of the challenge.

This commit is contained in:
Luke Else 2023-12-14 16:50:33 +00:00
parent f287d5a7ee
commit 9fb69c573b
5 changed files with 1464 additions and 9 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#

View File

@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#

View File

@ -20,7 +20,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Box::new(day10::Day10 {}), Box::new(day10::Day10 {}),
Box::new(day11::Day11 {}), Box::new(day11::Day11 {}),
Box::new(day12::Day12 {}), Box::new(day12::Day12 {}),
// 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 {}),

View File

@ -1,3 +1,5 @@
use itertools::Itertools;
use super::Solution; use super::Solution;
pub struct Day13 {} pub struct Day13 {}
@ -5,16 +7,28 @@ pub struct Day13 {}
impl Solution for Day13 { impl Solution for Day13 {
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 binding = input.clone().iter().join("\n");
let grids = binding
.split("\n\n")
.map(|s| s.split("\n").map(|l| l.as_bytes()).collect::<Vec<_>>())
.collect::<Vec<_>>();
Ok(Box::new(self.solve(&grids, 0)))
} }
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 binding = input.clone().iter().join("\n");
let grids = binding
.split("\n\n")
.map(|s| s.split("\n").map(|l| l.as_bytes()).collect::<Vec<_>>())
.collect::<Vec<_>>();
Ok(Box::new(self.solve(&grids, 1)))
} }
fn get_day(&self) -> u8 { fn get_day(&self) -> u8 {
@ -22,7 +36,53 @@ impl Solution for Day13 {
} }
} }
impl Day13 {} impl Day13 {
// Finds the index of the column where the reflection is located
fn find_col_reflection(&self, grid: &[&[u8]], limit: usize) -> Option<usize> {
(0..grid[0].len() - 1).find(|&c| {
let incorrect = (0..=c.min(grid[0].len() - c - 2))
.map(|dc| {
let a = c - dc;
let b = c + 1 + dc;
(0..grid.len())
.filter(|&r| grid[r][a] != grid[r][b])
.count()
})
.sum::<usize>();
incorrect == limit
})
}
// Finds the index of the row where the reflection is located
fn find_row_reflection(&self, grid: &[&[u8]], limit: usize) -> Option<usize> {
(0..grid.len() - 1).find(|&r| {
let incorrect = (0..=r.min(grid.len() - r - 2))
.map(|dr| {
let a = r - dr;
let b = r + 1 + dr;
(0..grid[0].len())
.filter(|&c| grid[a][c] != grid[b][c])
.count()
})
.sum::<usize>();
incorrect == limit
})
}
// Gets the given score for a reflection
// where limit is the index of the reflection you want to find in each image
fn solve(&self, grids: &[Vec<&[u8]>], limit: usize) -> usize {
grids
.iter()
.map(|grid| {
self.find_row_reflection(grid, limit)
.map(|r| (r + 1) * 100)
.or_else(|| self.find_col_reflection(grid, limit).map(|c| c + 1))
.unwrap()
})
.sum()
}
}
/// Test from puzzle input /// Test from puzzle input
#[cfg(test)] #[cfg(test)]
@ -44,7 +104,7 @@ mod test {
.unwrap() .unwrap()
.to_string(); .to_string();
assert_eq!(answer, "Ready"); assert_eq!(answer, "405");
} }
#[test] #[test]
@ -61,6 +121,6 @@ mod test {
.unwrap() .unwrap()
.to_string(); .to_string();
assert_eq!(answer, "Ready"); assert_eq!(answer, "400");
} }
} }