-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
47 lines (37 loc) · 1.11 KB
/
day8.py
File metadata and controls
47 lines (37 loc) · 1.11 KB
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
from lib.input import get_input
from lib.map import Map
raw_input = get_input(2024, 8)
map = Map(raw_input)
antennas = set(raw_input.replace("\n", "").replace(".", ""))
def iterate_antennas(map):
for char in antennas:
all_pos = map.find_all(char)
if len(all_pos) < 2:
continue
for pos1 in all_pos:
for pos2 in all_pos:
if pos1 == pos2:
continue
yield pos1, pos2
def part1():
antinodes = set()
for p1, p2 in iterate_antennas(map):
anode1 = p1 + (p1 - p2)
if map.is_in_bounds(anode1):
antinodes.add(anode1)
anode2 = p2 + (p2 - p1)
if map.is_in_bounds(anode2):
antinodes.add(anode2)
return len(antinodes)
def part2():
antinodes = set()
for p1, p2 in iterate_antennas(map):
anode1 = p1
while map.is_in_bounds(anode1):
antinodes.add(anode1)
anode1 += p1 - p2
anode2 = p2
while map.is_in_bounds(anode2):
antinodes.add(anode2)
anode2 += p2 - p1
return len(antinodes)