170 lines
3.4 KiB
Rust
170 lines
3.4 KiB
Rust
use aoc_runner_derive::{aoc, aoc_generator};
|
|
use grid::{Coord2d, Grid};
|
|
use std::str::FromStr;
|
|
|
|
#[aoc_generator(day3)]
|
|
fn parse(input: &str) -> Grid<u8> {
|
|
Grid::from_str(input).unwrap()
|
|
}
|
|
|
|
#[aoc(day3, part1)]
|
|
fn part1(input: &Grid<u8>) -> u64 {
|
|
let mut squares: u64 = 0;
|
|
|
|
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 tree in input.find_all(&b'#') {
|
|
for size in 1..(biggest_square - tree.x as usize) {
|
|
if input
|
|
.get(&(tree + &Coord2d { x: size as i64, y: 0 }))
|
|
.is_some_and(|c| *c == b'#')
|
|
&& input
|
|
.get(&(tree + &Coord2d { y: size as i64, x: 0 }))
|
|
.is_some_and(|c| *c == b'#')
|
|
&& input
|
|
.get(
|
|
&(tree
|
|
+ &Coord2d {
|
|
x: size as i64,
|
|
y: size as i64,
|
|
}),
|
|
)
|
|
.is_some_and(|c| *c == b'#')
|
|
{
|
|
squares += 1
|
|
}
|
|
}
|
|
}
|
|
|
|
squares
|
|
}
|
|
|
|
// 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,
|
|
Coord2d {
|
|
x: initial.x - initial.y,
|
|
y: initial.x + initial.y,
|
|
},
|
|
Coord2d {
|
|
x: -initial.y,
|
|
y: initial.x,
|
|
},
|
|
]
|
|
}
|
|
|
|
#[aoc(day3, part2)]
|
|
fn part2(input: &Grid<u8>) -> u64 {
|
|
let biggest_square = std::cmp::min(input.width(), input.height());
|
|
|
|
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;
|
|
rotated_offsets(Coord2d { x: *x_ofs, y: y_ofs })
|
|
.iter()
|
|
.all(|ofs| input.get(&(tree + ofs)) == Some(&b'#'))
|
|
})
|
|
.count() as u64
|
|
})
|
|
.sum::<u64>()
|
|
})
|
|
.sum()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use rstest::rstest;
|
|
|
|
#[rstest]
|
|
#[case(
|
|
"#.###
|
|
.##.#
|
|
##.##
|
|
.#.#.
|
|
.##.#",
|
|
1u64
|
|
)]
|
|
#[case(
|
|
".##..
|
|
.##.#
|
|
.....
|
|
#.##.
|
|
..##.",
|
|
2u64
|
|
)]
|
|
#[case(
|
|
"#####
|
|
#####
|
|
#####
|
|
#####
|
|
#####",
|
|
30u64
|
|
)]
|
|
fn part1_example(#[case] input: &str, #[case] expected: u64) {
|
|
assert_eq!(part1(&parse(input)), expected);
|
|
}
|
|
|
|
#[rstest]
|
|
#[case(
|
|
".#...
|
|
....#
|
|
.....
|
|
#....
|
|
...#.",
|
|
1
|
|
)]
|
|
#[case(
|
|
"..#..
|
|
.....
|
|
#...#
|
|
.....
|
|
..#..",
|
|
1
|
|
)]
|
|
#[case(
|
|
"....#..
|
|
.......
|
|
#......
|
|
.......
|
|
......#
|
|
.......
|
|
..#....",
|
|
1
|
|
)]
|
|
#[case(
|
|
"#.###
|
|
.##.#
|
|
##.##
|
|
.#.#.
|
|
.##.#",
|
|
5
|
|
)]
|
|
#[case(
|
|
"#######
|
|
#######
|
|
#######
|
|
#######
|
|
#######
|
|
#######
|
|
#######",
|
|
196
|
|
)]
|
|
fn part2_example(#[case] input: &str, #[case] expected: u64) {
|
|
assert_eq!(part2(&parse(input)), expected);
|
|
}
|
|
}
|