54 lines
1.7 KiB
Rust
54 lines
1.7 KiB
Rust
use crate::solvers::Solver;
|
|
use grid::Grid;
|
|
use std::io::BufRead;
|
|
|
|
pub struct PrintingDepartment {}
|
|
|
|
impl PrintingDepartment {
|
|
const PAPER_ROLL_CHAR: char = '@';
|
|
}
|
|
|
|
impl Solver for PrintingDepartment {
|
|
const PUZZLE_INDEX: u8 = 4;
|
|
const PUZZLE_NAME: &'static str = "Printing Department";
|
|
|
|
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
|
|
let mut grid = Grid::new(0, 0);
|
|
for line in reader.lines().map_while(Result::ok) {
|
|
grid.push_row(
|
|
line.bytes()
|
|
.map(|c| c == Self::PAPER_ROLL_CHAR as u8)
|
|
.collect(),
|
|
);
|
|
}
|
|
|
|
let mut part1 = 0;
|
|
'cells: for cell in grid.indexed_iter() {
|
|
if *cell.1 {
|
|
let mut count = 0;
|
|
// TODO: This could be a loop over eight directions instead.
|
|
for i in -1i32..=1 {
|
|
for j in -1i32..=1 {
|
|
if i != 0 || j != 0 {
|
|
if let Some(&blocked) = grid.get(
|
|
i32::try_from(cell.0.0).unwrap() + i,
|
|
i32::try_from(cell.0.1).unwrap() + j,
|
|
) {
|
|
if blocked {
|
|
if count <= 2 {
|
|
count += 1;
|
|
} else {
|
|
continue 'cells;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
part1 += 1;
|
|
}
|
|
}
|
|
(part1, 0)
|
|
}
|
|
}
|