-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_8.py
87 lines (71 loc) · 2.24 KB
/
day_8.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
from datetime import datetime
time_start = datetime.now()
with open('day_8.input', 'r') as file:
lines = file.readlines()
antennas = {}
for y in range(len(lines)):
for x in range(len(lines[0]) - 1):
val = lines[y][x]
if val != ".":
if val in antennas:
antennas[val].append((x,y))
else:
antennas[val] = [(x,y)]
# print("Keys", antennas.keys())
# ######## PART 1
def add_combination(x, y, array):
if 0 <= x < len(lines[0]) - 1 and 0 <= y < len(lines):
array.append((x, y))
combinations = []
for key, value in antennas.items():
for i in range(len(value)):
for j in range(i + 1, len(value)):
x1, y1 = value[i]
x2, y2 = value[j]
dx = x1 - x2
dy = y1 - y2
add_combination(x1 + dx, y1 + dy, combinations)
add_combination(x2 - dx, y2 - dy, combinations)
# print("Combinations:", combinations)
matrix = []
for line in lines:
matrix.append(list(line))
count = 0
for x,y in combinations:
if matrix[y][x] != "#":
count += 1
matrix[y][x] = "#"
# for row in matrix:
# print(" ".join(map(str, row[:-1])))
# print("Total part1", count)
# ######## PART 2
def add_resonate_combination(x, y, array, dx, dy):
while 0 <= x < len(lines[0]) - 1 and 0 <= y < len(lines):
array.append((x, y))
x += dx
y += dy
new_combinations = []
for key, value in antennas.items():
for i in range(len(value)):
for j in range(i + 1, len(value)):
x1, y1 = value[i]
x2, y2 = value[j]
dx = x1 - x2
dy = y1 - y2
add_resonate_combination(x1 + dx, y1 + dy, new_combinations, dx, dy)
add_resonate_combination(x2 - dx, y2 - dy, new_combinations, -dx, -dy)
matrix = []
for line in lines:
matrix.append(list(line))
# exclude all antennas and add later
count = 0
for x,y in new_combinations:
if matrix[y][x] == ".":
count += 1
matrix[y][x] = "#"
# for row in matrix:
# print(" ".join(map(str, row[:-1])))
print("Total part1", count)
print("Total part2", count + sum([len(sublist) for sublist in antennas.values()]))
time_end = datetime.now()
print("Total time: ", time_end - time_start)