-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.py
111 lines (82 loc) · 2.21 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
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
103
104
105
106
107
108
109
110
111
import sys
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
grid = []
max_x = 0
max_y = 0
antennas = {}
antinodes = set()
antinodes2 = set()
with open(inputfile,"r") as input:
grid = [line.rstrip() for line in input]
for i,x in enumerate(grid):
for j, y in enumerate(x):
if not y == ".":
if y in antennas:
antennas[y].append((i,j))
else:
antennas[y] = [(i,j)]
max_x = len(grid)-1
max_y = len(grid[0])-1
def find_antinodes(one,two):
global antinodes
difference = (one[0]-two[0], one[1]-two[1])
new1 = (one[0]+difference[0],one[1]+difference[1])
new2 = (two[0]-difference[0],two[1]-difference[1])
if 0 <= new1[0] <= max_x and 0 <= new1[1] <= max_y:
antinodes.add(new1)
if 0 <= new2[0] <= max_x and 0 <= new2[1] <= max_y:
antinodes.add(new2)
print(str(one)+"//"+str(two)+"=>"+str(difference)+"=>"+str(new1)+"//"+str(new2))
def find_antinodes2(one,two):
global antinodes2
difference = (one[0]-two[0], one[1]-two[1])
added = True
i = -1
while added:
i += 1
new1 = (one[0]+(difference[0]*i),one[1]+(difference[1]*i))
new2 = (two[0]-(difference[0]*i),two[1]-(difference[1]*i))
added = False
if 0 <= new1[0] <= max_x and 0 <= new1[1] <= max_y:
antinodes2.add(new1)
added = True
if 0 <= new2[0] <= max_x and 0 <= new2[1] <= max_y:
antinodes2.add(new2)
added = True
def run_part1():
# for line in grid:
# print(line)
print(antennas)
print(max_y)
print(max_y)
for fre in antennas:
process = antennas[fre].copy()
while len(process) > 1:
first = process.pop(0)
for pair in process:
find_antinodes(first,pair)
print(antinodes)
print(len(antinodes))
def run_part2():
for fre in antennas:
process = antennas[fre].copy()
while len(process) > 1:
first = process.pop(0)
for pair in process:
find_antinodes2(first,pair)
print(antinodes2)
print(len(antinodes2))
# for i,line in enumerate(grid):
# pr = ""
# for j,y in enumerate(line):
# if (i,j) in antinodes2:
# pr += "#"
# else:
# pr += y
# print(pr)
run_part1()
run_part2()