Files
aoc/2023/src/day13.rs
T
2026-12-02 15:05:55 -07:00

189 lines
5.1 KiB
Rust

use aoc_runner_derive::{aoc, aoc_generator};
use std::ops::Index;
// PARSE
#[derive(Debug)]
struct Pattern {
mirrors: Vec<Vec<char>>,
}
impl Pattern {
fn width(&self) -> usize {
self.mirrors[0].len()
}
fn height(&self) -> usize {
self.mirrors.len()
}
fn col(&self, idx: usize) -> Vec<char> {
self.mirrors.iter().map(|row| row[idx]).collect()
}
fn find_horizontal_reflection(&self) -> Option<usize> {
(1..self.height()).find(|idx| self.is_horizontal_reflection(*idx))
}
fn is_horizontal_reflection(&self, idx: usize) -> bool {
let row_pairs = std::cmp::min(idx, self.height() - idx);
(0..row_pairs).all(|offset| self.mirrors[idx - offset - 1] == self.mirrors[idx + offset])
}
fn find_vertical_reflection(&self) -> Option<usize> {
(1..self.width()).find(|idx| self.is_vertical_reflection(*idx))
}
fn is_vertical_reflection(&self, idx: usize) -> bool {
let col_pairs = std::cmp::min(idx, self.width() - idx);
(0..col_pairs).all(|offset| self.col(idx - offset - 1) == self.col(idx + offset))
}
fn find_smudged_horizontal_reflection(&self) -> Option<usize> {
(1..self.height()).find(|idx| self.is_smudged_horizontal_reflection(*idx))
}
fn is_smudged_horizontal_reflection(&self, idx: usize) -> bool {
// Same algorithm for problem 2, but count errors breaking reflection.
// If the count == 1 after all checks, then it is our smudge
let row_pairs = std::cmp::min(idx, self.height() - idx);
(0..row_pairs)
.map(|offset| {
self.mirrors[idx - offset - 1]
.iter()
.zip(self.mirrors[idx + offset].iter())
.filter(|(a, b)| **a != **b)
.count()
})
.sum::<usize>()
== 1
}
fn find_smudged_vertical_reflection(&self) -> Option<usize> {
(1..self.width()).find(|idx| self.is_smudged_vertical_reflection(*idx))
}
fn is_smudged_vertical_reflection(&self, idx: usize) -> bool {
let col_pairs = std::cmp::min(idx, self.width() - idx);
(0..col_pairs)
.map(|offset| {
self.col(idx - offset - 1)
.iter()
.zip(self.col(idx + offset).iter())
.filter(|(a, b)| **a != **b)
.count()
})
.sum::<usize>()
== 1
}
fn reflection_cost(&self) -> u64 {
if let Some(refl) = self.find_horizontal_reflection() {
100 * refl as u64
} else if let Some(refl) = self.find_vertical_reflection() {
refl as u64
} else {
panic!("no reflection");
}
}
fn smudged_reflection_cost(&self) -> u64 {
if let Some(refl) = self.find_smudged_horizontal_reflection() {
100 * refl as u64
} else if let Some(refl) = self.find_smudged_vertical_reflection() {
refl as u64
} else {
panic!("no reflection");
}
}
}
#[derive(Debug)]
struct Patterns {
patterns: Vec<Pattern>,
}
impl From<&str> for Patterns {
fn from(input: &str) -> Self {
let mut lines = input.lines();
let mut patterns = Vec::new();
let mut cur_lines = Vec::new();
while let Some(line) = lines.next() {
if line.len() == 0 {
patterns.push(Pattern { mirrors: cur_lines });
cur_lines = Vec::new();
} else {
cur_lines.push(line.chars().collect());
}
}
patterns.push(Pattern { mirrors: cur_lines });
Self { patterns }
}
}
// PROBLEM 1 solution
#[aoc_generator(day13)]
fn parse(input: &str) -> Patterns {
Patterns::from(input)
}
#[aoc(day13, part1)]
fn part1(patterns: &Patterns) -> u64 {
patterns.patterns.iter().map(|pat| pat.reflection_cost()).sum()
}
// PROBLEM 2 solution
#[aoc(day13, part2)]
fn part2(patterns: &Patterns) -> u64 {
patterns.patterns.iter().map(|pat| pat.smudged_reflection_cost()).sum()
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = "#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#";
#[test]
fn problem1_find_horizontal() {
let patterns = Patterns::from(EXAMPLE);
assert_eq!(patterns.patterns[1].find_horizontal_reflection(), Some(4));
}
#[test]
fn problem1_find_vertical() {
let patterns = Patterns::from(EXAMPLE);
assert_eq!(patterns.patterns[0].find_vertical_reflection(), Some(5));
}
#[test]
fn part1_example() {
assert_eq!(part1(&parse(EXAMPLE)), 405);
}
#[test]
fn problem2_find_horizontal() {
let patterns = Patterns::from(EXAMPLE);
assert_eq!(patterns.patterns[0].find_smudged_horizontal_reflection(), Some(3));
assert_eq!(patterns.patterns[1].find_smudged_horizontal_reflection(), Some(1));
}
#[test]
fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 400);
}
}