From a29992472b14cdd0c6738af70a0fae29a186c250 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Wed, 13 Dec 2023 15:34:17 +0100 Subject: day13 solution 2 --- src/bin/day13.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 8 deletions(-) (limited to 'src/bin/day13.rs') diff --git a/src/bin/day13.rs b/src/bin/day13.rs index 6f6ac97..02045ea 100644 --- a/src/bin/day13.rs +++ b/src/bin/day13.rs @@ -1,12 +1,14 @@ +use std::collections::HashSet; + static DAY: u8 = 13; fn main() { let input = advent::read_lines(DAY); println!("{DAY}a: {}", summarize_patterns(&input)); - println!("{DAY}b: {}", 0); + println!("{DAY}b: {}", summarize_patterns_with_smudge(&input)); } -#[derive(Clone,Copy)] +#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] enum Reflection { Vertical(usize), Horizontal(usize), @@ -15,12 +17,13 @@ enum Reflection { impl Reflection { fn summary(&self) -> usize { match *self { - Reflection::Vertical(column) => column, - Reflection::Horizontal(row) => row * 100, + Reflection::Vertical(column) => column + 1, + Reflection::Horizontal(row) => (row + 1) * 100, } } } +#[derive(Clone)] struct Map { pattern: Vec>, } @@ -38,6 +41,20 @@ impl Map { Map { pattern } } + fn _print_map(&self) { + for y in 0 .. self.pattern.len() { + for x in 0 .. self.pattern[0].len() { + if self.pattern[y][x] { + print!("#"); + } else { + print!("."); + } + } + println!(); + } + println!(); + } + fn is_horizontal_reflection(&self, y: usize) -> bool { for pair in (y+1 .. self.pattern.len()).zip((0 ..= y).rev()) { if self.pattern[pair.0] != self.pattern[pair.1] { @@ -58,17 +75,34 @@ impl Map { true } - fn find_reflection(&self) -> Reflection { + fn find_reflections(&self) -> HashSet { + let mut reflections = HashSet::new(); for y in 0 .. self.pattern.len() - 1 { if self.is_horizontal_reflection(y) { - return Reflection::Horizontal(y+1) + reflections.insert(Reflection::Horizontal(y)); } } for x in 0 .. self.pattern[0].len() - 1 { if self.is_vertical_reflection(x) { - return Reflection::Vertical(x+1) + reflections.insert(Reflection::Vertical(x)); } } + reflections + } + + fn find_reflection_with_smudge(&self) -> Reflection { + let orig_reflection = self.find_reflections(); + for y in 0 .. self.pattern.len() { + for x in 0 .. self.pattern[0].len() { + let mut map = self.clone(); + map.pattern[y][x] = !map.pattern[y][x]; + let reflections = map.find_reflections(); + let new_reflections = reflections.difference(&orig_reflection).collect::>(); + if !new_reflections.is_empty() { + return *new_reflections[0]; + } + } + } panic!("no reflection found"); } } @@ -76,7 +110,14 @@ impl Map { fn summarize_patterns(input: &[String]) -> usize { input.split(|line| line.is_empty()) .map(Map::new) - .map(|m| m.find_reflection().summary()) + .map(|m| m.find_reflections().iter().next().unwrap().summary()) + .sum() +} + +fn summarize_patterns_with_smudge(input: &[String]) -> usize { + input.split(|line| line.is_empty()) + .map(Map::new) + .map(|m| m.find_reflection_with_smudge().summary()) .sum() } @@ -104,5 +145,6 @@ mod tests { "#....#..#", ].iter().map(|&x| String::from(x)).collect::>(); assert_eq!(summarize_patterns(&input), 405); + assert_eq!(summarize_patterns_with_smudge(&input), 400); } } -- cgit v1.2.3