1use crate::util::grid::*;
5use crate::util::hash::*;
6use crate::util::point::*;
7
8type Input = (Grid<u8>, FastMap<u8, Vec<Point>>);
9
10pub fn parse(input: &str) -> Input {
11 let grid = Grid::parse(input);
12 let mut antennas = FastMap::new();
13
14 for point in grid.points() {
15 let frequency = grid[point];
16
17 if frequency != b'.' {
18 antennas.entry(frequency).or_insert_with(Vec::new).push(point);
19 }
20 }
21
22 (grid, antennas)
23}
24
25pub fn part1(input: &Input) -> u32 {
26 let (grid, antennas) = input;
27 let mut locations = grid.same_size_with(0);
28
29 for frequency in antennas.values() {
30 for &first in frequency {
31 for &second in frequency {
32 if first != second {
33 let distance = second - first;
34 let antinode = second + distance;
35
36 if grid.contains(antinode) {
37 locations[antinode] = 1;
38 }
39 }
40 }
41 }
42 }
43
44 locations.bytes.iter().sum()
45}
46
47pub fn part2(input: &Input) -> u32 {
48 let (grid, antennas) = input;
49 let mut locations = grid.same_size_with(0);
50
51 for frequency in antennas.values() {
52 for &first in frequency {
53 for &second in frequency {
54 if first != second {
55 let distance = second - first;
56 let mut antinode = second;
57
58 while grid.contains(antinode) {
59 locations[antinode] = 1;
60 antinode += distance;
61 }
62 }
63 }
64 }
65 }
66
67 locations.bytes.iter().sum()
68}