-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_2.py
43 lines (35 loc) · 1.5 KB
/
8_2.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
def main():
lines = read_input('input/8_2.txt')
width = len(lines[0])
height = len(lines)
nodes = dict()
antinodes = set()
for line_num, line in enumerate(lines):
for line_pos, node in enumerate(line):
if node == '.':
continue
if node not in nodes:
nodes[node] = list()
nodes[node].append((line_num, line_pos))
for node, positions in nodes.items():
for pos_idx, position in enumerate(positions):
antinodes.add(position)
if pos_idx == len(positions):
continue
for next_position in positions[pos_idx + 1:]:
diff = ((next_position[0] - position[0]), (next_position[1] - position[1]))
antinode_a = (position[0] - diff[0], position[1] - diff[1])
while 0 <= antinode_a[0] < height and 0 <= antinode_a[1] < width:
antinodes.add(antinode_a)
antinode_a = (antinode_a[0] - diff[0], antinode_a[1] - diff[1])
antinode_b = (next_position[0] + diff[0], next_position[1] + diff[1])
while 0 <= antinode_b[0] < height and 0 <= antinode_b[1] < width:
antinodes.add(antinode_b)
antinode_b = (antinode_b[0] + diff[0], antinode_b[1] + diff[1])
print(len(antinodes))
def read_input(file_path):
with open(file_path) as f:
lines = f.read().splitlines()
return lines
if __name__ == "__main__":
main()