day7: part2 + clippies

This commit is contained in:
2025-12-07 00:10:29 -08:00
parent f99f4e766a
commit 8f3ac2aacd
3 changed files with 51 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

View File

@@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN --> <!-- AOC TILES BEGIN -->
<h1 align="center"> <h1 align="center">
2025 - 12 ⭐ - Rust 2025 - 14 ⭐ - Rust
</h1> </h1>
<a href="src/day1.rs"> <a href="src/day1.rs">
<img src=".aoc_tiles/tiles/2025/01.png" width="161px"> <img src=".aoc_tiles/tiles/2025/01.png" width="161px">
@@ -20,4 +20,7 @@
<a href="src/day6.rs"> <a href="src/day6.rs">
<img src=".aoc_tiles/tiles/2025/06.png" width="161px"> <img src=".aoc_tiles/tiles/2025/06.png" width="161px">
</a> </a>
<a href="src/day7.rs">
<img src=".aoc_tiles/tiles/2025/07.png" width="161px">
</a>
<!-- AOC TILES END --> <!-- AOC TILES END -->

View File

@@ -1,26 +1,19 @@
use std::collections::HashSet;
use std::str::FromStr;
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
use grid::{AsCoord2d, Grid}; use grid::{AsCoord2d, Grid};
use itertools::Itertools;
use std::str::FromStr;
#[aoc_generator(day7)] #[aoc_generator(day7)]
fn parse(input: &str) -> Grid<u8> { 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: impl AsCoord2d) -> u64 {
match grid.get(&pos) { match grid.set(&pos, b'|') {
None | Some(b'|') => 0, None | Some(b'|') => 0,
Some(b'.') | Some(b'S') => { Some(b'.') | Some(b'S') => emit_beams(grid, (pos.x(), pos.y() + 1)),
grid.set(&pos, b'|');
emit_beams(grid, &(pos.x(), pos.y() + 1))
}
Some(b'^') => { Some(b'^') => {
grid.set(&pos, b'|'); 1 + emit_beams(grid, (pos.x() - 1, pos.y())) + emit_beams(grid, (pos.x() + 1, pos.y()))
1 + emit_beams(grid, &(pos.x() - 1, pos.y()))
+ emit_beams(grid, &(pos.x() + 1, pos.y()))
} }
_ => panic!(), _ => panic!(),
} }
@@ -34,13 +27,48 @@ fn part1(input: &Grid<u8>) -> u64 {
#[aoc(day7, part2)] #[aoc(day7, part2)]
fn part2(input: &Grid<u8>) -> u64 { fn part2(input: &Grid<u8>) -> u64 {
0 let mut grid = Grid::same_shape(input, 0u64);
grid.set(&input.find(&b'S').unwrap(), 1);
// Start row is already set up
for y in 1..grid.height() {
let row_above = grid.row_iter(y as i64 - 1).unwrap().cloned().collect_vec();
// for each column with non-zero counts in the row above get its column index and count
for (x, n) in row_above.into_iter().enumerate().filter(|(_i, n)| *n > 0) {
match input.get(&(x, y)) {
None => panic!("How did we end up outside the grid?"),
// if our current position is a caret, add that count to our neighbours
Some(b'^') => {
grid.set(&(x - 1, y), grid.get(&(x - 1, y)).unwrap() + n);
grid.set(&(x + 1, y), grid.get(&(x + 1, y)).unwrap() + n);
}
// if our current position is a . add that count here
Some(b'.') => {
grid.set(&(x, y), grid.get(&(x, y)).unwrap() + n);
}
_ => panic!(),
}
}
}
grid.row(grid.height() as i64 - 1)
.unwrap()
.iter()
.sum::<u64>()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
const TRIVIAL_EXAMPLE: &str = "...S...
.......
...^...
..^.^.^
...^.^.
.......
.......";
const EXAMPLE: &str = ".......S....... const EXAMPLE: &str = ".......S.......
............... ...............
.......^....... .......^.......
@@ -65,6 +93,11 @@ mod tests {
#[test] #[test]
fn part2_example() { fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 0); assert_eq!(part2(&parse(EXAMPLE)), 40);
}
#[test]
fn part2_trivial_example() {
assert_eq!(part2(&parse(TRIVIAL_EXAMPLE)), 7);
} }
} }