-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8_1.py
49 lines (37 loc) · 1.24 KB
/
day8_1.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
import time
start_time = time.time()
from collections import defaultdict
from utils import parse_file_to_2d_matrix, print_board
is_test = False
day = 8
input_file = f'./input/day{day}{"_test" if is_test else ""}.txt'
grid = {(x, y): e for x, line in enumerate(open(input_file).readlines()) for y, e in enumerate(line.strip())}
all_antennas = defaultdict(list)
for k, v in grid.items():
if v not in '.':
all_antennas[v].append(k)
total = 0
uniq_antis = set()
for antenna, poss in all_antennas.items():
for i in range(len(poss)):
for j in range(i+1, len(poss)):
x1, y1 = poss[i]
x2, y2 = poss[j]
d_x = (x1 - x2)
d_y = (y1 - y2)
anti1 = (x1 + d_x, y1 + d_y)
if anti1 in grid:
if anti1 not in uniq_antis:
uniq_antis.add(anti1)
total += 1
anti2 = (x2 - d_x, y2 - d_y)
if anti2 in grid:
if anti2 not in uniq_antis:
uniq_antis.add(anti2)
total += 1
print(total)
# mat = parse_file_to_2d_matrix('./input/day8_test.txt')
# for x, y in uniq_antis:
# mat[x][y] = '#'
# print_board(mat)
print(f't2 = {time.time() - start_time:.6f}s')