From 94d4fc2fc3a181bd79f92eb6d93c833e88e6e802 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 13 Dec 2023 00:57:17 -0500 Subject: day 13 --- src/bin/2023/day13.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/bin/2023/main.rs | 2 + 2 files changed, 104 insertions(+) create mode 100644 src/bin/2023/day13.rs (limited to 'src/bin') diff --git a/src/bin/2023/day13.rs b/src/bin/2023/day13.rs new file mode 100644 index 0000000..a5579b8 --- /dev/null +++ b/src/bin/2023/day13.rs @@ -0,0 +1,102 @@ +#![allow(dead_code)] +#![allow(unused_variables)] + +use advent_of_code::prelude::*; + +enum Mirror { + Horizontal(usize), + Vertical(usize), +} + +impl Mirror { + fn score(&self) -> usize { + match self { + Self::Horizontal(n) => 100 * n, + Self::Vertical(n) => *n, + } + } +} + +fn find_mirror(pattern: &Grid, smudges: usize) -> Mirror { + 'mirror: for row in pattern.each_row().skip(1) { + let mut found_smudges = 0; + for offset in 0..row.min(pattern.rows() - row.0).0 { + let a = pattern.row_vec(row + offset); + let b = pattern.row_vec(row - offset - 1); + found_smudges += a + .into_iter() + .zip(b.into_iter()) + .filter(|(a, b)| a != b) + .count(); + if found_smudges > smudges { + continue 'mirror; + } + } + if found_smudges != smudges { + continue 'mirror; + } + return Mirror::Horizontal(row.0); + } + + 'mirror: for col in pattern.each_col().skip(1) { + let mut found_smudges = 0; + for offset in 0..col.min(pattern.cols() - col.0).0 { + let a = pattern.col_vec(col + offset); + let b = pattern.col_vec(col - offset - 1); + found_smudges += a + .into_iter() + .zip(b.into_iter()) + .filter(|(a, b)| a != b) + .count(); + if found_smudges > smudges { + continue 'mirror; + } + } + if found_smudges != smudges { + continue 'mirror; + } + return Mirror::Vertical(col.0); + } + + unreachable!() +} + +pub fn parse(fh: File) -> Result>> { + let mut lines = parse::raw_lines(fh).peekable(); + let mut grids = vec![]; + while lines.peek().is_some() { + grids + .push(parse::grid(parse::chunk(&mut lines), |c, _, _| c == b'#')); + } + Ok(grids) +} + +pub fn part1(patterns: Vec>) -> Result { + Ok(patterns + .into_iter() + .map(|pattern| find_mirror(&pattern, 0).score()) + .sum::() + .try_into() + .unwrap()) +} + +pub fn part2(patterns: Vec>) -> Result { + Ok(patterns + .into_iter() + .map(|pattern| find_mirror(&pattern, 1).score()) + .sum::() + .try_into() + .unwrap()) +} + +#[test] +fn test() { + assert_eq!( + part1(parse(parse::data(2023, 13).unwrap()).unwrap()).unwrap(), + 37975 + ); + assert_eq!( + part2(parse(parse::data(2023, 13).unwrap()).unwrap()).unwrap(), + 32497 + ); +} diff --git a/src/bin/2023/main.rs b/src/bin/2023/main.rs index 591fdc8..2ee1dc4 100644 --- a/src/bin/2023/main.rs +++ b/src/bin/2023/main.rs @@ -23,6 +23,7 @@ mod day9; mod day10; mod day11; mod day12; +mod day13; // NEXT MOD #[paw::main] @@ -41,6 +42,7 @@ fn main(opt: Opt) -> Result<()> { 10 => advent_of_code::day!(2023, opt.day, opt.puzzle, day10), 11 => advent_of_code::day!(2023, opt.day, opt.puzzle, day11), 12 => advent_of_code::day!(2023, opt.day, opt.puzzle, day12), + 13 => advent_of_code::day!(2023, opt.day, opt.puzzle, day13), // NEXT PART _ => panic!("unknown day {}", opt.day), } -- cgit v1.2.3-54-g00ecf