day 13 part 1 start coding
This commit is contained in:
parent
b5185d38b2
commit
4e186f6221
2 changed files with 98 additions and 0 deletions
15
inputs/day13.txt
Normal file
15
inputs/day13.txt
Normal file
|
@ -0,0 +1,15 @@
|
|||
#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#
|
83
src/bin/day13.rs
Normal file
83
src/bin/day13.rs
Normal file
|
@ -0,0 +1,83 @@
|
|||
#![forbid(elided_lifetimes_in_paths, unsafe_code)]
|
||||
|
||||
use aoc23::read;
|
||||
use chumsky::{prelude::*, text::int};
|
||||
use rayon::iter::{IntoParallelRefIterator as _, ParallelIterator as _};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
enum Cell {
|
||||
Working,
|
||||
Broken,
|
||||
Unknown
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
fn parser() -> impl Parser<char, Self, Error = Simple<char>> {
|
||||
choice((
|
||||
just('.').map(|_| Self::Working),
|
||||
just('#').map(|_| Self::Broken),
|
||||
just('?').map(|_| Self::Unknown)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
struct Pattern {
|
||||
grid: Vec<Vec<Cell>>
|
||||
}
|
||||
|
||||
impl Pattern {
|
||||
fn parser() -> impl Parser<char, Self, Error = Simple<char>> {
|
||||
Cell::parser()
|
||||
.repeated()
|
||||
.at_least(1)
|
||||
.then_ignore(just("\n"))
|
||||
.repeated()
|
||||
.at_least(1)
|
||||
.map(|grid| Self { grid })
|
||||
}
|
||||
}
|
||||
|
||||
fn parser() -> impl Parser<char, Vec<Pattern>, Error = Simple<char>> {
|
||||
Pattern::parser()
|
||||
.separated_by(just("\n"))
|
||||
.then_ignore(end())
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let patterns = read("inputs/day13.txt", parser())?;
|
||||
|
||||
let mut rows = Vec::new();
|
||||
let mut cols = Vec::new();
|
||||
for Pattern { grid } in &patterns {
|
||||
'i: for i in 1 .. grid.len() - 1 {
|
||||
for j in 0 .. i.min(grid.len() - i) {
|
||||
if grid[i - j - 1] != grid[i + j + 1] {
|
||||
eprintln!("discarding cut {i} due to rows {}/{}", i - j - 1, i + j);
|
||||
continue 'i;
|
||||
}
|
||||
}
|
||||
rows.push(i);
|
||||
}
|
||||
println!("rows: {rows:?}");
|
||||
|
||||
'i: for i in 1 .. grid[0].len() - 1 {
|
||||
for j in 0 .. i.min(grid[0].len() - i) {
|
||||
for k in 0 .. grid.len() {
|
||||
if grid[k][i - j - 1] != grid[k][i + j] {
|
||||
eprintln!("discarding cut {i} due to cols {}/{}", i - j - 1, i + j);
|
||||
continue 'i;
|
||||
}
|
||||
}
|
||||
}
|
||||
cols.push(i);
|
||||
}
|
||||
println!("cols: {cols:?}");
|
||||
}
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
rows.iter().sum::<usize>() + 100 * cols.iter().sum::<usize>()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue