2026/day3: improve test cases, an iter chain is about twice as fast on part2

This commit is contained in:
ktims committed 2026-12-02 23:24:33 -07:00
1 parent e9bbc38534
commit f896774877
1 file changed
+46 -36
+46 -36
View File
@@ -13,7 +13,7 @@ fn part1(input: &Grid<u8>) -> u64 {
let biggest_square = std::cmp::min(input.width(), input.height()); let biggest_square = std::cmp::min(input.width(), input.height());
// For each tree in the input grid, look at possible squares right and down from it // For each tree in the input grid, look at possible squares right and down from it.
for tree in input.find_all(&b'#') { for tree in input.find_all(&b'#') {
for size in 1..(biggest_square - tree.x as usize) { for size in 1..(biggest_square - tree.x as usize) {
if input if input
@@ -40,7 +40,10 @@ fn part1(input: &Grid<u8>) -> u64 {
squares squares
} }
// Given the offset of the first (of 3) other square corners, return the full set // Given the offset of the top right square corners, return the full set.
// TR = (x, y)
// BR = (x-y, x+y)
// BL = (-y, x)
fn rotated_offsets(initial: Coord2d) -> [Coord2d; 3] { fn rotated_offsets(initial: Coord2d) -> [Coord2d; 3] {
[ [
initial, initial,
@@ -57,32 +60,28 @@ fn rotated_offsets(initial: Coord2d) -> [Coord2d; 3] {
#[aoc(day3, part2)] #[aoc(day3, part2)]
fn part2(input: &Grid<u8>) -> u64 { fn part2(input: &Grid<u8>) -> u64 {
let mut squares: u64 = 0;
let biggest_square = std::cmp::min(input.width(), input.height()); let biggest_square = std::cmp::min(input.width(), input.height());
// println!("{input}"); input
for tree in input.find_all(&b'#') { .find_all(&b'#')
// println!("START {tree}"); .map(|tree| {
for size in 1..=biggest_square { // for each tree in the input
// println!(" len: {size}"); (1..biggest_square)
for x_ofs in 1..=size as i64 { .map(|size| {
// for offset totals from 1..biggest_square
(1..=size as i64)
.filter(|x_ofs| {
// count the candidates which hit at all 4 corners
let y_ofs = size as i64 - x_ofs; let y_ofs = size as i64 - x_ofs;
// print!(" + ({x_ofs}, {y_ofs}) - "); rotated_offsets(Coord2d { x: *x_ofs, y: y_ofs })
if rotated_offsets(Coord2d { x: x_ofs, y: y_ofs })
.iter() .iter()
// .inspect(|c| print!("{c}"))
.all(|ofs| input.get(&(tree + ofs)) == Some(&b'#')) .all(|ofs| input.get(&(tree + ofs)) == Some(&b'#'))
{ })
// print!(" - hit"); .count() as u64
squares += 1; })
} .sum::<u64>()
// println!() })
} .sum()
}
}
squares
} }
#[cfg(test)] #[cfg(test)]
@@ -90,28 +89,31 @@ mod tests {
use super::*; use super::*;
use rstest::rstest; use rstest::rstest;
const EXAMPLE1: &str = "#.### #[rstest]
#[case(
"#.###
.##.# .##.#
##.## ##.##
.#.#. .#.#.
.##.#"; .##.#",
1u64
const EXAMPLE2: &str = ".##.. )]
#[case(
".##..
.##.# .##.#
..... .....
#.##. #.##.
..##."; ..##.",
2u64
const EXAMPLE3: &str = "##### )]
#[case(
"#####
##### #####
##### #####
##### #####
#####"; #####",
30u64
#[rstest] )]
#[case(EXAMPLE1, 1u64)]
#[case(EXAMPLE2, 2u64)]
#[case(EXAMPLE3, 30u64)]
fn part1_example(#[case] input: &str, #[case] expected: u64) { fn part1_example(#[case] input: &str, #[case] expected: u64) {
assert_eq!(part1(&parse(input)), expected); assert_eq!(part1(&parse(input)), expected);
} }
@@ -143,6 +145,14 @@ mod tests {
..#....", ..#....",
1 1
)] )]
#[case(
"#.###
.##.#
##.##
.#.#.
.##.#",
5
)]
#[case( #[case(
"####### "#######
####### #######