day7: remember to use some convenience functions from my grid

This commit is contained in:
2025-12-07 00:27:23 -08:00
parent 8f3ac2aacd
commit 30644bcbfc

View File

@@ -1,5 +1,5 @@
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
use grid::{AsCoord2d, Grid}; use grid::{Coord2d, Grid};
use itertools::Itertools; use itertools::Itertools;
use std::str::FromStr; use std::str::FromStr;
@@ -8,13 +8,11 @@ fn parse(input: &str) -> Grid<u8> {
Grid::from_str(input).unwrap() Grid::from_str(input).unwrap()
} }
fn emit_beams(grid: &mut Grid<u8>, pos: impl AsCoord2d) -> u64 { fn emit_beams(grid: &mut Grid<u8>, pos: Coord2d) -> u64 {
match grid.set(&pos, b'|') { match grid.set(&pos, b'|') {
None | Some(b'|') => 0, None | Some(b'|') => 0,
Some(b'.') | Some(b'S') => emit_beams(grid, (pos.x(), pos.y() + 1)), Some(b'.') | Some(b'S') => emit_beams(grid, pos + &(0, 1)),
Some(b'^') => { Some(b'^') => 1 + emit_beams(grid, pos + &(-1, 0)) + emit_beams(grid, pos + &(1, 0)),
1 + emit_beams(grid, (pos.x() - 1, pos.y())) + emit_beams(grid, (pos.x() + 1, pos.y()))
}
_ => panic!(), _ => panic!(),
} }
} }
@@ -39,12 +37,12 @@ fn part2(input: &Grid<u8>) -> u64 {
None => panic!("How did we end up outside the grid?"), None => panic!("How did we end up outside the grid?"),
// if our current position is a caret, add that count to our neighbours // if our current position is a caret, add that count to our neighbours
Some(b'^') => { Some(b'^') => {
grid.set(&(x - 1, y), grid.get(&(x - 1, y)).unwrap() + n); grid.increment(&(x - 1, y), n);
grid.set(&(x + 1, y), grid.get(&(x + 1, y)).unwrap() + n); grid.increment(&(x + 1, y), n);
} }
// if our current position is a . add that count here // if our current position is a . add that count here
Some(b'.') => { Some(b'.') => {
grid.set(&(x, y), grid.get(&(x, y)).unwrap() + n); grid.increment(&(x, y), n);
} }
_ => panic!(), _ => panic!(),
} }