2026/day3: improve test cases, an iter chain is about twice as fast on part2
This commit is contained in:
1 file changed
+46
-36
+46
-36
@@ -13,7 +13,7 @@ fn part1(input: &Grid<u8>) -> u64 {
|
||||
|
||||
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 size in 1..(biggest_square - tree.x as usize) {
|
||||
if input
|
||||
@@ -40,7 +40,10 @@ fn part1(input: &Grid<u8>) -> u64 {
|
||||
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] {
|
||||
[
|
||||
initial,
|
||||
@@ -57,32 +60,28 @@ fn rotated_offsets(initial: Coord2d) -> [Coord2d; 3] {
|
||||
|
||||
#[aoc(day3, part2)]
|
||||
fn part2(input: &Grid<u8>) -> u64 {
|
||||
let mut squares: u64 = 0;
|
||||
|
||||
let biggest_square = std::cmp::min(input.width(), input.height());
|
||||
|
||||
// println!("{input}");
|
||||
for tree in input.find_all(&b'#') {
|
||||
// println!("START {tree}");
|
||||
for size in 1..=biggest_square {
|
||||
// println!(" len: {size}");
|
||||
for x_ofs in 1..=size as i64 {
|
||||
input
|
||||
.find_all(&b'#')
|
||||
.map(|tree| {
|
||||
// for each tree in the input
|
||||
(1..biggest_square)
|
||||
.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;
|
||||
// print!(" + ({x_ofs}, {y_ofs}) - ");
|
||||
if rotated_offsets(Coord2d { x: x_ofs, y: y_ofs })
|
||||
rotated_offsets(Coord2d { x: *x_ofs, y: y_ofs })
|
||||
.iter()
|
||||
// .inspect(|c| print!("{c}"))
|
||||
.all(|ofs| input.get(&(tree + ofs)) == Some(&b'#'))
|
||||
{
|
||||
// print!(" - hit");
|
||||
squares += 1;
|
||||
}
|
||||
// println!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
squares
|
||||
})
|
||||
.count() as u64
|
||||
})
|
||||
.sum::<u64>()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -90,28 +89,31 @@ mod tests {
|
||||
use super::*;
|
||||
use rstest::rstest;
|
||||
|
||||
const EXAMPLE1: &str = "#.###
|
||||
#[rstest]
|
||||
#[case(
|
||||
"#.###
|
||||
.##.#
|
||||
##.##
|
||||
.#.#.
|
||||
.##.#";
|
||||
|
||||
const EXAMPLE2: &str = ".##..
|
||||
.##.#",
|
||||
1u64
|
||||
)]
|
||||
#[case(
|
||||
".##..
|
||||
.##.#
|
||||
.....
|
||||
#.##.
|
||||
..##.";
|
||||
|
||||
const EXAMPLE3: &str = "#####
|
||||
..##.",
|
||||
2u64
|
||||
)]
|
||||
#[case(
|
||||
"#####
|
||||
#####
|
||||
#####
|
||||
#####
|
||||
#####";
|
||||
|
||||
#[rstest]
|
||||
#[case(EXAMPLE1, 1u64)]
|
||||
#[case(EXAMPLE2, 2u64)]
|
||||
#[case(EXAMPLE3, 30u64)]
|
||||
#####",
|
||||
30u64
|
||||
)]
|
||||
fn part1_example(#[case] input: &str, #[case] expected: u64) {
|
||||
assert_eq!(part1(&parse(input)), expected);
|
||||
}
|
||||
@@ -143,6 +145,14 @@ mod tests {
|
||||
..#....",
|
||||
1
|
||||
)]
|
||||
#[case(
|
||||
"#.###
|
||||
.##.#
|
||||
##.##
|
||||
.#.#.
|
||||
.##.#",
|
||||
5
|
||||
)]
|
||||
#[case(
|
||||
"#######
|
||||
#######
|
||||
|
||||
Reference in new issue
Block a user