day 13 part 2
This commit is contained in:
parent
caced29a1f
commit
cbbee06a64
1 changed files with 40 additions and 36 deletions
|
@ -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}");
|
|
||||||
'i: for i in 1 .. grid.len() {
|
|
||||||
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);
|
|
||||||
continue 'i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println!("mirroring at row {i}");
|
|
||||||
rows.push(i);
|
|
||||||
}
|
|
||||||
// println!("rows: {rows:?}");
|
|
||||||
|
|
||||||
'i: for i in 1 .. grid[0].len() {
|
for Pattern { grid } in patterns {
|
||||||
for j in 0 .. i.min(grid[0].len() - i) {
|
'i: for i in 1 .. grid.len() {
|
||||||
for row in grid {
|
let mut d = 0;
|
||||||
if row[i - j - 1] != row[i + j] {
|
for j in 0 .. i.min(grid.len() - i) {
|
||||||
// eprintln!(
|
assert_eq!(grid[0].len(), grid[i - j - 1].len());
|
||||||
// "discarding cut {i} due to cols {}/{}",
|
assert_eq!(grid[0].len(), grid[i + j].len());
|
||||||
// i - j - 1,
|
for k in 0 .. grid[0].len() {
|
||||||
// i + j
|
if grid[i - j - 1][k] != grid[i + j][k] {
|
||||||
// );
|
d += 1;
|
||||||
continue 'i;
|
if d > diff {
|
||||||
|
continue 'i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("mirroring at col {i}");
|
if d == diff {
|
||||||
cols.push(i);
|
rows.push(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// println!("cols: {cols:?}");
|
|
||||||
|
|
||||||
// break;
|
'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] {
|
||||||
|
d += 1;
|
||||||
|
if d > diff {
|
||||||
|
continue 'i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if d == diff {
|
||||||
|
cols.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 20464 is too low
|
rows.iter().sum::<usize>() * 100 + cols.iter().sum::<usize>()
|
||||||
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue