-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8pt1.py
100 lines (75 loc) · 1.98 KB
/
day8pt1.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from day8input import day8input
# print(day8input)
# check the matrix for antennas of same frequency
#
# navigate thr the matrix and store all antennas as a dictionary adding the coords
rows = len(day8input)
cols = len(day8input[0])
m_antinodes_hash = set()
antinodes_hash = set()
def is_inbounds(a):
return (0 <= a[0] and a[0] < rows) and (0 <= a[1] < cols)
def coord_minus(a):
dx = a[0]
dy = a[1]
return (-dx, -dy)
def coord_sum(a,b):
dx = a[0] + b[0]
dy = a[1] + b[1]
return (dx, dy)
def check_antinode(antinode, antinodes_set=antinodes_hash):
is_inside = is_inbounds(antinode)
if is_inside:
antinodes_set.add(antinode)
return is_inside
def check_antinodes(ant, diff):
ant_a = ant
while is_inbounds(ant_a):
check_antinode(ant_a, m_antinodes_hash)
ant_a = coord_sum(ant_a, diff)
def validate_pairs(antennas_coords):
import itertools
antennas_pairs = itertools.combinations(antennas_coords, 2)
total = 0
for ants_pair in antennas_pairs:
ants_c = [tuple(map(int, ant_c.split(','))) for ant_c in ants_pair]
ant_a = ants_c[0]
ant_b = ants_c[1]
#
diff = coord_sum(ant_a, coord_minus(ant_b))
# Pt1
check_antinode(
coord_sum(ant_a, diff)
)
#
check_antinodes(ant_a, diff)
ant_a = ant_b
diff = coord_minus(diff)
# Pt2
check_antinode(
coord_sum(ant_a, diff)
)
#
check_antinodes(ant_a, diff)
def get_antennas_dict(matrix=day8input):
antennas_dict = dict()
for i in range(rows):
for j in range(cols):
cell = matrix[i][j]
if cell != '.':
# print(cell)
if cell not in antennas_dict:
antennas_dict[cell] = []
cell_index = str(i)+','+str(j)
antennas_dict[cell].append(cell_index)
return antennas_dict
if __name__ == '__main__':
for a_index, antenna_coords in get_antennas_dict().items():
print(antenna_coords, a_index)
# calculate pairs
validate_pairs(antenna_coords)
print(len(antinodes_hash))
assert(len(antinodes_hash) == 252)
print(len(m_antinodes_hash))
assert(len(m_antinodes_hash) == 839)
# 252 and 839