201 lines
4.4 KiB
Rust
201 lines
4.4 KiB
Rust
use aoc_runner_derive::{aoc, aoc_generator};
|
|
use grid::{Coord2d, Grid};
|
|
use itertools::Itertools;
|
|
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,
|
|
},
|
|
]
|
|
}
|
|
|
|
// Generate the other two corners of a square given the top left and top right coordinates, in clockwise order
|
|
fn other_two(a: &Coord2d, b: &Coord2d) -> [Coord2d; 2] {
|
|
let ofs = b - a;
|
|
[
|
|
Coord2d {
|
|
x: a.x + (ofs.x - ofs.y),
|
|
y: a.y + (ofs.x + ofs.y),
|
|
},
|
|
Coord2d {
|
|
x: a.x - ofs.y,
|
|
y: a.y + ofs.x,
|
|
},
|
|
]
|
|
}
|
|
|
|
#[aoc(day3, part2, Pairwise)]
|
|
fn part2_pairwise(input: &Grid<u8>) -> u64 {
|
|
let trees: Vec<Coord2d> = input.find_all(&b'#').collect();
|
|
return trees
|
|
.iter()
|
|
.tuple_combinations::<(_, _)>()
|
|
.filter(|(a, b)| (*b - *a).x >= 0 && (*b - *a).y > 0)
|
|
.map(|(a, b)| other_two(&a, &b))
|
|
.filter(|corners| corners.iter().all(|pos| input.get(&pos) == Some(&b'#')))
|
|
.count() as u64;
|
|
}
|
|
|
|
#[aoc(day3, part2, Brute)]
|
|
fn part2_brute(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,
|
|
#[values(part2_brute, part2_pairwise)] version: fn(&Grid<u8>) -> u64,
|
|
) {
|
|
assert_eq!(version(&parse(input)), expected);
|
|
}
|
|
}
|