From 2381795637fde3781bd9e10b679a856c61ffe2a5 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Wed, 13 Dec 2023 14:04:31 +0100 Subject: day13 solution 1 --- src/bin/day13.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/bin/day13.rs (limited to 'src/bin/day13.rs') diff --git a/src/bin/day13.rs b/src/bin/day13.rs new file mode 100644 index 0000000..6f6ac97 --- /dev/null +++ b/src/bin/day13.rs @@ -0,0 +1,108 @@ +static DAY: u8 = 13; + +fn main() { + let input = advent::read_lines(DAY); + println!("{DAY}a: {}", summarize_patterns(&input)); + println!("{DAY}b: {}", 0); +} + +#[derive(Clone,Copy)] +enum Reflection { + Vertical(usize), + Horizontal(usize), +} + +impl Reflection { + fn summary(&self) -> usize { + match *self { + Reflection::Vertical(column) => column, + Reflection::Horizontal(row) => row * 100, + } + } +} + +struct Map { + pattern: Vec>, +} + +impl Map { + fn new(input: &[String]) -> Map { + let mut pattern = Vec::new(); + for line in input { + let row = line.chars() + .map(|c| c == '#') + .collect(); + pattern.push(row); + } + + Map { pattern } + } + + 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] { + return false; + } + } + true + } + + fn is_vertical_reflection(&self, x: usize) -> bool { + for pair in (x+1 .. self.pattern[0].len()).zip((0 ..= x).rev()) { + for y in 0 .. self.pattern.len() { + if self.pattern[y][pair.0] != self.pattern[y][pair.1] { + return false; + } + } + } + true + } + + fn find_reflection(&self) -> Reflection { + for y in 0 .. self.pattern.len() - 1 { + if self.is_horizontal_reflection(y) { + return Reflection::Horizontal(y+1) + } + } + for x in 0 .. self.pattern[0].len() - 1 { + if self.is_vertical_reflection(x) { + return Reflection::Vertical(x+1) + } + } + panic!("no reflection found"); + } +} + +fn summarize_patterns(input: &[String]) -> usize { + input.split(|line| line.is_empty()) + .map(Map::new) + .map(|m| m.find_reflection().summary()) + .sum() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test() { + let input = [ + "#.##..##.", + "..#.##.#.", + "##......#", + "##......#", + "..#.##.#.", + "..##..##.", + "#.#.##.#.", + "", + "#...##..#", + "#....#..#", + "..##..###", + "#####.##.", + "#####.##.", + "..##..###", + "#....#..#", + ].iter().map(|&x| String::from(x)).collect::>(); + assert_eq!(summarize_patterns(&input), 405); + } +} -- cgit v1.2.3