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:
parent
f287d5a7ee
commit
9fb69c573b
1365
src/input/day13
1365
src/input/day13
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,15 @@
|
||||
#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#
|
@ -0,0 +1,15 @@
|
||||
#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#
|
@ -20,7 +20,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
Box::new(day10::Day10 {}),
|
||||
Box::new(day11::Day11 {}),
|
||||
Box::new(day12::Day12 {}),
|
||||
// Box::new(day13::Day13 {}),
|
||||
Box::new(day13::Day13 {}),
|
||||
// Box::new(day14::Day14 {}),
|
||||
// Box::new(day15::Day15 {}),
|
||||
// Box::new(day16::Day16 {}),
|
||||
|
@ -1,3 +1,5 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
use super::Solution;
|
||||
|
||||
pub struct Day13 {}
|
||||
@ -5,16 +7,28 @@ pub struct Day13 {}
|
||||
impl Solution for Day13 {
|
||||
fn part1(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> 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(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> 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 {
|
||||
@ -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
|
||||
#[cfg(test)]
|
||||
@ -44,7 +104,7 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "405");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -61,6 +121,6 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "400");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user