2024-12-11 15:45:52 -08:00
|
|
|
use aoc_runner_derive::{aoc, aoc_generator};
|
2024-12-07 21:43:56 -08:00
|
|
|
use grid::Grid;
|
|
|
|
use itertools::Itertools;
|
2024-12-12 14:01:04 -08:00
|
|
|
use rustc_hash::FxHashSet;
|
2024-12-12 02:14:29 -08:00
|
|
|
use std::io::BufRead;
|
2024-12-07 21:43:56 -08:00
|
|
|
|
2024-12-12 14:01:04 -08:00
|
|
|
type HashSet<T> = FxHashSet<T>;
|
|
|
|
|
2024-12-11 15:45:52 -08:00
|
|
|
#[aoc_generator(day8)]
|
|
|
|
pub fn get_input(input: &[u8]) -> AntennaMap {
|
2024-12-12 02:14:29 -08:00
|
|
|
AntennaMap::from(input)
|
2024-12-07 21:43:56 -08:00
|
|
|
}
|
|
|
|
|
2024-12-11 15:45:52 -08:00
|
|
|
pub struct AntennaMap {
|
2024-12-07 21:43:56 -08:00
|
|
|
map: Grid<u8>,
|
|
|
|
}
|
|
|
|
|
2024-12-12 02:14:29 -08:00
|
|
|
impl<T: BufRead> From<T> for AntennaMap {
|
|
|
|
fn from(input: T) -> Self {
|
2024-12-07 22:49:57 -08:00
|
|
|
Self { map: Grid::from(input) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AntennaMap {
|
|
|
|
fn find_antinodes(&self, start: usize, reps: Option<usize>) -> Grid<bool> {
|
|
|
|
let mut antinodes = Grid::with_shape(self.map.width(), self.map.height(), false);
|
|
|
|
// find the unique frequencies in a dumb way
|
|
|
|
// NOTE: The dumb way is faster than the slightly-smarter ways I tried
|
|
|
|
let freq_set: HashSet<&u8> = HashSet::from_iter(self.map.data.iter().filter(|c| **c != b'.'));
|
|
|
|
|
|
|
|
// for each unique frequency, get all the pairs' positions
|
|
|
|
for freq in freq_set {
|
|
|
|
for pair in self
|
|
|
|
.map
|
|
|
|
.data
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|(_, c)| *c == freq)
|
|
|
|
.map(|(i, _)| self.map.coord(i as i64).unwrap())
|
|
|
|
.permutations(2)
|
|
|
|
{
|
|
|
|
// permutations generates both pairs, ie. ((1,2),(2,1)) and ((2,1),(1,2)) so we don't need
|
|
|
|
// to consider the 'negative' side of the line, which will be generated by the other pair
|
|
|
|
let (a, b) = (pair[0], pair[1]);
|
|
|
|
let offset = (a.0 - b.0, a.1 - b.1);
|
2024-12-11 15:45:52 -08:00
|
|
|
for i in (start..).map_while(|i| if Some(i - start) != reps { Some(i as i64) } else { None }) {
|
2024-12-07 22:49:57 -08:00
|
|
|
let node_pos = (a.0 + i * offset.0, a.1 + i * offset.1);
|
2024-12-11 18:43:13 -08:00
|
|
|
if antinodes.set(&node_pos, true).is_none() {
|
2024-12-07 22:49:57 -08:00
|
|
|
// left the grid
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-07 21:43:56 -08:00
|
|
|
}
|
2024-12-07 22:49:57 -08:00
|
|
|
antinodes
|
2024-12-07 21:43:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PROBLEM 1 solution
|
2024-12-11 15:45:52 -08:00
|
|
|
#[aoc(day8, part1)]
|
|
|
|
pub fn part1(map: &AntennaMap) -> u64 {
|
2024-12-07 22:49:57 -08:00
|
|
|
let antinodes = map.find_antinodes(1, Some(1));
|
2024-12-11 18:43:13 -08:00
|
|
|
antinodes.count(&true) as u64
|
2024-12-07 21:43:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// PROBLEM 2 solution
|
2024-12-11 15:45:52 -08:00
|
|
|
#[aoc(day8, part2)]
|
|
|
|
pub fn part2(map: &AntennaMap) -> u64 {
|
2024-12-07 22:49:57 -08:00
|
|
|
let antinodes = map.find_antinodes(0, None);
|
2024-12-11 18:43:13 -08:00
|
|
|
antinodes.count(&true) as u64
|
2024-12-07 21:43:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-12-11 15:45:52 -08:00
|
|
|
use crate::day8::*;
|
2024-12-07 21:43:56 -08:00
|
|
|
|
2024-12-11 15:45:52 -08:00
|
|
|
const EXAMPLE: &[u8] = b"............
|
2024-12-07 21:43:56 -08:00
|
|
|
........0...
|
|
|
|
.....0......
|
|
|
|
.......0....
|
|
|
|
....0.......
|
|
|
|
......A.....
|
|
|
|
............
|
|
|
|
............
|
|
|
|
........A...
|
|
|
|
.........A..
|
|
|
|
............
|
|
|
|
............";
|
|
|
|
|
|
|
|
#[test]
|
2024-12-11 15:45:52 -08:00
|
|
|
fn part1_example() {
|
|
|
|
assert_eq!(part1(&get_input(EXAMPLE)), 14);
|
2024-12-07 21:43:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-12-11 15:45:52 -08:00
|
|
|
fn part2_example() {
|
|
|
|
assert_eq!(part2(&get_input(EXAMPLE)), 34);
|
2024-12-07 21:43:56 -08:00
|
|
|
}
|
|
|
|
}
|