|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::iter::successors; |
| 3 | +use std::ops::Range; |
| 4 | + |
| 5 | +use aoclp::positioning::pt::{matrix_to_map, Pt}; |
| 6 | +use aoclp::solvers_impl::input::safe_get_input_as_terrain; |
| 7 | +use itertools::Itertools; |
| 8 | + |
| 9 | +pub fn part_1() -> usize { |
| 10 | + Map::default().antinodes(false).count() |
| 11 | +} |
| 12 | + |
| 13 | +pub fn part_2() -> usize { |
| 14 | + Map::default().antinodes(true).count() |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug)] |
| 18 | +struct Map { |
| 19 | + x_bounds: Range<i64>, |
| 20 | + y_bounds: Range<i64>, |
| 21 | + antennas: HashMap<Pt, char>, |
| 22 | +} |
| 23 | + |
| 24 | +impl Map { |
| 25 | + fn antinodes(&self, resonating: bool) -> impl Iterator<Item = Pt> { |
| 26 | + let antinodes = self |
| 27 | + .antennas |
| 28 | + .iter() |
| 29 | + .sorted_by_key(|&(_, &freq)| freq) |
| 30 | + .chunk_by(|&(_, &freq)| freq) |
| 31 | + .into_iter() |
| 32 | + .flat_map(|(_, antennas)| { |
| 33 | + let antennas = antennas.map(|(&pt, _)| pt).collect_vec(); |
| 34 | + let antinodes = antennas |
| 35 | + .iter() |
| 36 | + .cartesian_product(antennas.iter()) |
| 37 | + .filter(|(p1, p2)| p1 != p2) |
| 38 | + .flat_map(|(&p1, &p2)| { |
| 39 | + let antinodes = match resonating { |
| 40 | + true => self.resonating_antinodes_for(p1, p2).collect_vec(), |
| 41 | + false => self.dull_antinodes_for(p1, p2).collect_vec(), |
| 42 | + }; |
| 43 | + antinodes.into_iter() |
| 44 | + }) |
| 45 | + .collect_vec(); |
| 46 | + antinodes.into_iter() |
| 47 | + }) |
| 48 | + .unique() |
| 49 | + .filter(|pt| pt.within(self.x_bounds.clone(), self.y_bounds.clone())) |
| 50 | + .collect_vec(); |
| 51 | + antinodes.into_iter() |
| 52 | + } |
| 53 | + |
| 54 | + fn dull_antinodes_for(&self, p1: Pt, p2: Pt) -> impl Iterator<Item = Pt> { |
| 55 | + let diff = p1 - p2; |
| 56 | + let anti1 = p1 + diff; |
| 57 | + let anti2 = p2 - diff; |
| 58 | + [anti1, anti2].into_iter() |
| 59 | + } |
| 60 | + |
| 61 | + fn resonating_antinodes_for(&self, p1: Pt, p2: Pt) -> impl Iterator<Item = Pt> + use<'_> { |
| 62 | + let diff = p1 - p2; |
| 63 | + let from_p1 = successors(Some(p1), move |&pt| { |
| 64 | + let next = pt + diff; |
| 65 | + next.within(self.x_bounds.clone(), self.y_bounds.clone()) |
| 66 | + .then_some(next) |
| 67 | + }); |
| 68 | + let from_p2 = successors(Some(p2), move |&pt| { |
| 69 | + let next = pt - diff; |
| 70 | + next.within(self.x_bounds.clone(), self.y_bounds.clone()) |
| 71 | + .then_some(next) |
| 72 | + }); |
| 73 | + from_p1.chain(from_p2) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl Default for Map { |
| 78 | + fn default() -> Self { |
| 79 | + let antennas_matrix = input(); |
| 80 | + |
| 81 | + let y_bounds = 0..antennas_matrix.len() as i64; |
| 82 | + let x_bounds = 0..antennas_matrix[0].len() as i64; |
| 83 | + let antennas = matrix_to_map(antennas_matrix) |
| 84 | + .into_iter() |
| 85 | + .filter(|&(_, c)| c != '.') |
| 86 | + .collect(); |
| 87 | + |
| 88 | + Self { x_bounds, y_bounds, antennas } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn input() -> Vec<Vec<char>> { |
| 93 | + safe_get_input_as_terrain(2024, 8) |
| 94 | +} |
0 commit comments