solution: day 13 clean

This commit is contained in:
Matej Janezic 2023-12-13 23:15:21 +01:00
parent 000002707d
commit 00000280ae
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
1 changed files with 21 additions and 23 deletions

View File

@ -1,23 +1,22 @@
fn mirror_h(shape: &Vec<Vec<char>>, smudges: usize) -> Option<usize> { fn mirror_h(shape: &[Vec<char>], smudges: usize) -> Option<usize> {
(1..shape.len()) (1..shape.len()).find(|&i| {
.filter(|&i| { shape
shape .iter()
.iter() .skip(i)
.skip(i) .zip(shape.iter().take(i).rev())
.zip(shape.iter().take(i).rev()) .map(|(x, y)| {
.map(|(x, y)| { x.iter()
x.iter() .zip(y.iter())
.zip(y.iter()) .map(|(xx, yy)| (xx != yy) as usize)
.map(|(xx, yy)| (xx != yy) as usize) .sum::<usize>()
.sum::<usize>() })
}) .sum::<usize>()
.sum::<usize>() == smudges
== smudges })
})
.max()
} }
fn mirror_v(shape: &Vec<Vec<char>>, smudges: usize) -> Option<usize> {
let shape = (0..shape[0].len()) fn mirror_v(shape: &[Vec<char>], smudges: usize) -> Option<usize> {
let shape: Vec<Vec<char>> = (0..shape[0].len())
.map(|col| (0..shape.len()).map(|row| shape[row][col]).collect()) .map(|col| (0..shape.len()).map(|row| shape[row][col]).collect())
.collect(); .collect();
@ -28,10 +27,9 @@ fn solve(input: &str, smudges: usize) -> usize {
input input
.split("\n\n") .split("\n\n")
.map(|x| x.lines().map(|line| line.chars().collect()).collect()) .map(|x| x.lines().map(|line| line.chars().collect()).collect())
.map(|shape| { .map(|shape: Vec<Vec<char>>| {
let v = mirror_v(&shape, smudges).unwrap_or_default(); mirror_v(&shape, smudges).unwrap_or_default()
let h = mirror_h(&shape, smudges).unwrap_or_default(); + mirror_h(&shape, smudges).unwrap_or_default() * 100
v + h * 100
}) })
.sum() .sum()
} }