Repository navigation
Expand file tree
/
Copy pathday8.py
More file actions
102 lines (84 loc) · 2.97 KB
/
Copy pathday8.py
File metadata and controls
102 lines (84 loc) · 2.97 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
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
101
102
import sys
file = sys.argv[1]
with open(file) as f:
data = f.readlines()
for i in range(len(data)):
data[i] = list(data[i].strip())
antennas = {}
for i in range(len(data)):
for j in range(len(data[0])):
if data[i][j] != '.' and data[i][j] not in antennas.keys():
antennas[data[i][j]] = [i, j]
elif data[i][j] in antennas.keys():
antennas[data[i][j]] = antennas[data[i][j]] + [i, j]
def possible_antinodes(pos_1, pos_2, length, width):
diff_x = pos_2[0] - pos_1[0]
diff_y = pos_2[1] - pos_1[1]
antinodes = []
antinode_1 = []
antinode_2 = []
antinode_1.append(pos_1[0] - diff_x)
antinode_2.append(pos_2[0] + diff_x)
antinode_1.append(pos_1[1] - diff_y)
antinode_2.append(pos_2[1] + diff_y)
if antinode_1[0] >= 0 and antinode_1[1] >= 0:
if antinode_1[0] < length and antinode_1[1] < width:
antinodes.append(antinode_1)
if antinode_2[0] >= 0 and antinode_2[1] >= 0:
if antinode_2[0] < length and antinode_2[1] < width:
antinodes.append(antinode_2)
return antinodes
def harmonics_antinodes(pos_1, pos_2, length, width):
diff_x = pos_2[0] - pos_1[0]
diff_y = pos_2[1] - pos_1[1]
antinodes = []
#from pos_1
on_map = True
i = 1
while(on_map):
new_x = pos_1[0] - diff_x * i
new_y = pos_1[1] - diff_y * i
if new_x >= 0 and new_x < length and new_y >= 0 and new_y < width:
antinodes.append([new_x, new_y])
i += 1
else:
on_map = False
#from pos_2
on_map = True
i = 1
while(on_map):
new_x = pos_2[0] + diff_x * i
new_y = pos_2[1] + diff_y * i
if new_x >= 0 and new_x < length and new_y >= 0 and new_y < width:
antinodes.append([new_x, new_y])
i += 1
else:
on_map = False
return antinodes
result = 0
for i in range(len(antennas.keys())):
antenna = [*antennas][i]
positions = antennas[antenna]
for j in range(0, len(positions) - 2, 2):
for k in range(j + 2, len(positions), 2):
antinodes = possible_antinodes(positions[j:j+2], positions[k:k+2], len(data), len(data[0]))
for n in antinodes:
data[n[0]][n[1]] = '#'
for i in range(len(data)):
for j in range(len(data[0])):
if data[i][j] == '#': result += 1
print('Part one: ', result)
result = 0
for i in range(len(antennas.keys())):
antenna = [*antennas][i]
positions = antennas[antenna]
for j in range(0, len(positions) - 2, 2):
for k in range(j + 2, len(positions), 2):
antinodes = harmonics_antinodes(positions[j:j+2], positions[k:k+2], len(data), len(data[0]))
for n in antinodes:
data[n[0]][n[1]] = '#'
for i in range(len(data)):
for j in range(len(data[0])):
if data[i][j] == '#': result += 1
if data[i][j] in antennas: result += 1
print('Part two: ', result)