day 13 part 2

This commit is contained in:
Dominic 2023-12-13 18:35:38 +01:00
parent caced29a1f
commit cbbee06a64
Signed by: msrd0
GPG key ID: DCC8C247452E98F9

View file

@ -1,4 +1,3 @@
// #![allow(clippy::never_loop)]
#![forbid(elided_lifetimes_in_paths, unsafe_code)]
use aoc23::read;
@ -43,51 +42,56 @@ fn parser() -> impl Parser<char, Vec<Pattern>, Error = Simple<char>> {
.then_ignore(end())
}
fn main() -> anyhow::Result<()> {
let patterns = read("inputs/day13.txt", parser())?;
fn find_mirrors_in_patters(patterns: &[Pattern], diff: u32) -> usize {
let mut rows = Vec::new();
let mut cols = Vec::new();
for (pidx, Pattern { grid }) in patterns.iter().enumerate() {
eprintln!("Pattern {pidx}");
for Pattern { grid } in patterns {
'i: for i in 1 .. grid.len() {
let mut d = 0;
for j in 0 .. i.min(grid.len() - i) {
if grid[i - j - 1] != grid[i + j] {
// eprintln!("discarding cut {i} due to rows {}/{}", i - j - 1, i + j);
assert_eq!(grid[0].len(), grid[i - j - 1].len());
assert_eq!(grid[0].len(), grid[i + j].len());
for k in 0 .. grid[0].len() {
if grid[i - j - 1][k] != grid[i + j][k] {
d += 1;
if d > diff {
continue 'i;
}
}
println!("mirroring at row {i}");
}
}
if d == diff {
rows.push(i);
}
// println!("rows: {rows:?}");
}
'i: for i in 1 .. grid[0].len() {
let mut d = 0;
for j in 0 .. i.min(grid[0].len() - i) {
for row in grid {
if row[i - j - 1] != row[i + j] {
// eprintln!(
// "discarding cut {i} due to cols {}/{}",
// i - j - 1,
// i + j
// );
d += 1;
if d > diff {
continue 'i;
}
}
}
println!("mirroring at col {i}");
}
if d == diff {
cols.push(i);
}
// println!("cols: {cols:?}");
// break;
}
}
// 20464 is too low
println!(
"{}",
rows.iter().sum::<usize>() * 100 + cols.iter().sum::<usize>()
);
}
fn main() -> anyhow::Result<()> {
let patterns = read("inputs/day13.txt", parser())?;
println!("{}", find_mirrors_in_patters(&patterns, 0));
println!("{}", find_mirrors_in_patters(&patterns, 1));
Ok(())
}