2024 - Day 8 (Resonant Collinearity)

View puzzle description on AdventOfCode.com
Part 1
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3

#Advent of Code
#2024 Day 8, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from collections import defaultdict
from util.two_d_world import World

def solve(puzzle_input: list[str]) -> int:
    area: World = World(".")
    area.load_from_lists(puzzle_input)

    antennas: defaultdict[str, list[tuple[int, int]]] = defaultdict(list)
    antinodes: set[tuple[int, int]] = set()

    for y in range(area.min_y, area.max_y + 1):
        for x in range(area.min_x, area.max_x + 1):
            frequency: str
            if (frequency := area[(x, y)]) != ".":
                other: tuple[int, int]
                for other in antennas[frequency]:
                    # dx and dy from other to current antenna
                    dx = x - other[0]
                    dy = y - other[1]
                    if dx == int(dx) and dy == int(dy):
                        if area.in_bounds(other[0] - dx, other[1] - dy):
                            antinodes.add((other[0] - dx, other[1] - dy))
                        if area.in_bounds(x + dx, y + dy):
                            antinodes.add((x + dx, y + dy))

                antennas[frequency].append((x, y))

    return len(antinodes)

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The number of unique locations that contain an antinode is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["..........",
                                       "..........",
                                       "..........",
                                       "....a.....",
                                       "..........",
                                       ".....a....",
                                       "..........",
                                       "..........",
                                       "..........",
                                       ".........."]), 2)

    def test_ex2(self):
        return self.assertEqual(solve(["..........",
                                       "..........",
                                       "..........",
                                       "....a.....",
                                       "........a.",
                                       ".....a....",
                                       "..........",
                                       "..........",
                                       "..........",
                                       ".........."]), 4)

    def test_ex3(self):
        return self.assertEqual(solve(["..........",
                                       "..........",
                                       "..........",
                                       "....a.....",
                                       "........a.",
                                       ".....a....",
                                       "..........",
                                       "......A...",
                                       "..........",
                                       ".........."]), 4)

    def test_ex4(self):
        return self.assertEqual(solve(["............",
                                       "........0...",
                                       ".....0......",
                                       ".......0....",
                                       "....0.......",
                                       "......A.....",
                                       "............",
                                       "............",
                                       "........A...",
                                       ".........A..",
                                       "............",
                                       "............"]), 14)

if __name__ == "__main__":
    run(main)
Part 2
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
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3

#Advent of Code
#2024 Day 8, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from collections import defaultdict
from util.two_d_world import World

def solve(puzzle_input: list[str]) -> int:
    area: World = World(".")
    area.load_from_lists(puzzle_input)

    antennas: defaultdict[str, list[tuple[int, int]]] = defaultdict(list)
    antinodes: set[tuple[int, int]] = set()

    for y in range(area.min_y, area.max_y + 1):
        for x in range(area.min_x, area.max_x + 1):
            frequency: str
            if (frequency := area[(x, y)]) != ".":
                other: tuple[int, int]
                for other in antennas[frequency]:
                    # dx and dy from other to current antenna
                    dx = x - other[0]
                    dy = y - other[1]
                    if dx == int(dx) and dy == int(dy):
                        u, v = other
                        while area.in_bounds(u, v):
                            antinodes.add((u, v))
                            u -= dx
                            v -= dy

                        u, v = x, y
                        while area.in_bounds(u, v):
                            antinodes.add((u, v))
                            u += dx
                            v += dy

                antennas[frequency].append((x, y))

    return len(antinodes)

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The number of unique locations that contain an antinode is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["T.........",
                                       "...T......",
                                       ".T........",
                                       "..........",
                                       "..........",
                                       "..........",
                                       "..........",
                                       "..........",
                                       "..........",
                                       ".........."]), 9)

    def test_ex2(self):
        return self.assertEqual(solve(["............",
                                       "........0...",
                                       ".....0......",
                                       ".......0....",
                                       "....0.......",
                                       "......A.....",
                                       "............",
                                       "............",
                                       "........A...",
                                       ".........A..",
                                       "............",
                                       "............"]), 34)

if __name__ == "__main__":
    run(main)
Return to solution index