-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.py
44 lines (39 loc) · 1.58 KB
/
day08.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
import math
from collections import defaultdict
import itertools
from typing import Dict, Set
with open("input.txt") as input_file:
input_lines = input_file.readlines()
input_lines = [line.strip('\n') for line in input_lines]
Point = complex
width = len(input_lines[0])
height = len(input_lines)
antennae_by_freq: Dict[str, Set[Point]] = defaultdict(set)
for r, line in enumerate(input_lines):
for c, char in enumerate(line):
if char != ".":
antennae_by_freq[char].add(r * 1j + c)
have_antinodes: Set[Point] = set()
for freq, antennae in antennae_by_freq.items():
for antenna_1, antenna_2 in itertools.combinations(antennae, 2):
delta = antenna_2 - antenna_1
antinode_1 = antenna_1 - delta
antinode_2 = antenna_2 + delta
for antinode in (antinode_1, antinode_2):
if 0 <= antinode.real < width and 0 <= antinode.imag < height:
have_antinodes.add(antinode)
answer_1 = len(have_antinodes)
print("Answer 1:", answer_1) # 303
have_antinodes: Set[Point] = set()
for freq, antennae in antennae_by_freq.items():
for antenna_1, antenna_2 in itertools.combinations(antennae, 2):
full_delta = antenna_2 - antenna_1
delta_gcd = math.gcd(int(full_delta.real), int(full_delta.imag))
delta = full_delta // delta_gcd
antinode = antenna_1
for delta in (delta, -delta):
while 0 <= antinode.real < width and 0 <= antinode.imag < height:
have_antinodes.add(antinode)
antinode += delta
answer_2 = len(have_antinodes)
print("Answer 2:", answer_2) # 1045