aoc/year2024/
day08.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! # Resonant Collinearity
//!
//! Antennas frequencies are grouped together to reduce the O(n²) pairwise comparisons.
use crate::util::grid::*;
use crate::util::hash::*;
use crate::util::point::*;

type Input = (Grid<u8>, FastMap<u8, Vec<Point>>);

pub fn parse(input: &str) -> Input {
    let grid = Grid::parse(input);
    let mut antennas = FastMap::new();

    for y in 0..grid.height {
        for x in 0..grid.width {
            let point = Point::new(x, y);
            let frequency = grid[point];

            if frequency != b'.' {
                antennas.entry(frequency).or_insert_with(Vec::new).push(point);
            }
        }
    }

    (grid, antennas)
}

pub fn part1(input: &Input) -> u32 {
    let (grid, antennas) = input;
    let mut locations = grid.same_size_with(0);

    for frequency in antennas.values() {
        for &first in frequency {
            for &second in frequency {
                if first != second {
                    let distance = second - first;
                    let antinode = second + distance;

                    if grid.contains(antinode) {
                        locations[antinode] = 1;
                    }
                }
            }
        }
    }

    locations.bytes.iter().sum()
}

pub fn part2(input: &Input) -> u32 {
    let (grid, antennas) = input;
    let mut locations = grid.same_size_with(0);

    for frequency in antennas.values() {
        for &first in frequency {
            for &second in frequency {
                if first != second {
                    let distance = second - first;
                    let mut antinode = second;

                    while grid.contains(antinode) {
                        locations[antinode] = 1;
                        antinode += distance;
                    }
                }
            }
        }
    }

    locations.bytes.iter().sum()
}