use aoc_runner_derive::{aoc, aoc_generator}; use grid::{AsCoord2d, Coord2d, Grid}; use itertools::Itertools; use std::{collections::BTreeSet, str::FromStr}; #[aoc_generator(day3)] fn parse(input: &str) -> Grid { Grid::from_str(input).unwrap() } #[aoc(day3, part1)] fn part1(input: &Grid) -> 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, }, ] } // Thought this might be faster, but it is _way_ slower. I think it requires smarter culling of combinations which will // get removed later as already found. I tried a simple cull which undercounted and still took 40x longer than the // 'bruteforce' approach. #[aoc(day3, part2, Pairwise)] fn part2_pairwise(input: &Grid) -> u64 { let mut combos: BTreeSet<_> = input.find_all(&b'#').tuple_combinations::<(_, _)>().collect(); let mut squares = 0; while let Some((a, b)) = combos.iter().next().copied() { let ofs = (b.x - a.x, b.y - a.y); let other_corners = rotated_offsets(ofs.to_coord()).map(|ofs| a + &ofs); for pair in [a, other_corners[0], other_corners[1], other_corners[2]] .iter() .permutations(2) { combos.remove(&(*pair[0], *pair[1])); } if other_corners.iter().all(|pos| input.get(&pos) == Some(&b'#')) { squares += 1; } } return squares; } #[aoc(day3, part2, Brute)] fn part2_brute(input: &Grid) -> 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::() }) .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) -> u64, ) { assert_eq!(version(&parse(input)), expected); } }