2026/day3: improve test cases, an iter chain is about twice as fast on part2
This commit is contained in:
1 file changed
+49
-39
+49
-39
@@ -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| {
|
||||||
let y_ofs = size as i64 - x_ofs;
|
// for offset totals from 1..biggest_square
|
||||||
// print!(" + ({x_ofs}, {y_ofs}) - ");
|
(1..=size as i64)
|
||||||
if rotated_offsets(Coord2d { x: x_ofs, y: y_ofs })
|
.filter(|x_ofs| {
|
||||||
.iter()
|
// count the candidates which hit at all 4 corners
|
||||||
// .inspect(|c| print!("{c}"))
|
let y_ofs = size as i64 - x_ofs;
|
||||||
.all(|ofs| input.get(&(tree + ofs)) == Some(&b'#'))
|
rotated_offsets(Coord2d { x: *x_ofs, y: y_ofs })
|
||||||
{
|
.iter()
|
||||||
// print!(" - hit");
|
.all(|ofs| input.get(&(tree + ofs)) == Some(&b'#'))
|
||||||
squares += 1;
|
})
|
||||||
}
|
.count() as u64
|
||||||
// println!()
|
})
|
||||||
}
|
.sum::<u64>()
|
||||||
}
|
})
|
||||||
}
|
.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(
|
||||||
"#######
|
"#######
|
||||||
#######
|
#######
|
||||||
|
|||||||
Reference in new issue
Block a user