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