-
Notifications
You must be signed in to change notification settings - Fork 0
/
dec08.py
85 lines (64 loc) · 2.2 KB
/
dec08.py
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
#!/usr/bin/env python
import itertools
from collections import defaultdict
from dataclasses import dataclass
from ibidem.advent_of_code.board import Board
from ibidem.advent_of_code.util import get_input_name
@dataclass(frozen=True)
class Position:
x: int
y: int
def __add__(self, other):
return Position(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Position(self.x - other.x, self.y - other.y)
def manhattan(self):
return abs(self.x) + abs(self.y)
def antinode(self, other):
path = self - other
return self + path
def load(fobj):
return Board.from_string(fobj.read(), growable=False)
def find_antinodes1(a, b, board):
pos = a.antinode(b)
if 0 <= pos.x < board.size_x and 0 <= pos.y < board.size_y:
yield pos
pos = b.antinode(a)
if 0 <= pos.x < board.size_x and 0 <= pos.y < board.size_y:
yield pos
def find_antinodes2(a, b, board):
path = a - b
pos = a + path
while 0 <= pos.x < board.size_x and 0 <= pos.y < board.size_y:
yield pos
pos = pos + path
def part1(board: Board):
frequencies = map_frequencies(board)
antinodes = set()
for freq in frequencies.keys():
for a, b in itertools.combinations(frequencies[freq], 2):
antinodes.update(find_antinodes1(a, b, board))
return len(antinodes)
def map_frequencies(board):
frequencies = defaultdict(list)
for y in range(board.size_y):
for x in range(board.size_x):
v = board.get(x, y)
if v != ".":
frequencies[v].append(Position(x, y))
return frequencies
def part2(board: Board):
frequencies = map_frequencies(board)
antinodes = set()
for freq in frequencies.keys():
for a, b in itertools.permutations(frequencies[freq], 2):
antinodes.update(find_antinodes2(a, b, board))
antinodes.add(a)
return len(antinodes)
if __name__ == "__main__":
with open(get_input_name(8, 2024)) as fobj:
p1_result = part1(load(fobj))
print(f"Part 1: {p1_result}")
with open(get_input_name(8, 2024)) as fobj:
p2_result = part2(load(fobj))
print(f"Part 2: {p2_result}")