From 000002508c2ab4276a720d46e3fadc387788fd6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Wed, 13 Dec 2023 14:44:59 +0100 Subject: [PATCH] solution: day 13 --- data/examples/13.txt | 15 ++++++++++ src/bin/13.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 data/examples/13.txt create mode 100644 src/bin/13.rs diff --git a/data/examples/13.txt b/data/examples/13.txt new file mode 100644 index 000000000..3b6b5cc --- /dev/null +++ b/data/examples/13.txt @@ -0,0 +1,15 @@ +#.##..##. +..#.##.#. +##......# +##......# +..#.##.#. +..##..##. +#.#.##.#. + +#...##..# +#....#..# +..##..### +#####.##. +#####.##. +..##..### +#....#..# diff --git a/src/bin/13.rs b/src/bin/13.rs new file mode 100644 index 000000000..71b8c18 --- /dev/null +++ b/src/bin/13.rs @@ -0,0 +1,66 @@ +fn mirror_h(shape: &Vec>, smudges: usize) -> Option { + (1..shape.len()) + .filter(|&i| { + shape + .iter() + .skip(i) + .zip(shape.iter().take(i).rev()) + .map(|(x, y)| { + x.iter() + .zip(y.iter()) + .map(|(xx, yy)| (xx != yy) as usize) + .sum::() + }) + .sum::() + == smudges + }) + .max() +} +fn mirror_v(shape: &Vec>, smudges: usize) -> Option { + let shape = (0..shape[0].len()) + .map(|col| (0..shape.len()).map(|row| shape[row][col]).collect()) + .collect(); + + mirror_h(&shape, smudges) +} + +fn solve(input: &str, smudges: usize) -> usize { + input + .split("\n\n") + .map(|x| x.lines().map(|line| line.chars().collect()).collect()) + .map(|shape| { + let v = mirror_v(&shape, smudges).unwrap_or_default(); + let h = mirror_h(&shape, smudges).unwrap_or_default(); + v + h * 100 + }) + .sum() +} + +pub fn part_one(input: &str) -> Option { + Some(solve(input, 0)) +} + +pub fn part_two(input: &str) -> Option { + Some(solve(input, 1)) +} + +aoc::solution!(13); + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_part_one() { + assert_eq!( + part_one(&aoc::template::read_file("examples", 13)), + Some(405) + ); + } + #[test] + fn test_part_two() { + assert_eq!( + part_two(&aoc::template::read_file("examples", 13)), + Some(400) + ); + } +}