-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.py
268 lines (227 loc) · 7.93 KB
/
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import math
import collections
from collections import Counter
import re
from tqdm import tqdm
# Advent of Code[About][Events][Shop][Settings][Log Out]Krish (AoC++) 14*
# λy.2024[Calendar][AoC++][Sponsors][Leaderboard][Stats]
# Our sponsors help make Advent of Code possible:
# Architus - Join us to build cool tech (and help elves) in Lithuania!
# --- Day 8: Resonant Collinearity ---
# You find yourselves on the roof of a top-secret Easter Bunny installation.
# While The Historians do their thing, you take a look at the familiar huge antenna. Much to your surprise, it seems to have been reconfigured to emit a signal that makes people 0.1% more likely to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift! Unthinkable!
# Scanning across the city, you find that there are actually many such antennas. Each antenna is tuned to a specific frequency indicated by a single lowercase letter, uppercase letter, or digit. You create a map (your puzzle input) of these antennas. For example:
# ............
# ........0...
# .....0......
# .......0....
# ....0.......
# ......A.....
# ............
# ............
# ........A...
# .........A..
# ............
# ............
# The signal only applies its nefarious effect at specific antinodes based on the resonant frequencies of the antennas. In particular, an antinode occurs at any point that is perfectly in line with two antennas of the same frequency - but only when one of the antennas is twice as far away as the other. This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them.
# So, for these two antennas with frequency a, they create the two antinodes marked with #:
# ..........
# ...#......
# ..........
# ....a.....
# ..........
# .....a....
# ..........
# ......#...
# ..........
# ..........
# Adding a third antenna with the same frequency creates several more antinodes. It would ideally add four antinodes, but two are off the right side of the map, so instead it adds only two:
# ..........
# ...#......
# #.........
# ....a.....
# ........a.
# .....a....
# ..#.......
# ......#...
# ..........
# ..........
# Antennas with different frequencies don't create antinodes; A and a count as different frequencies. However, antinodes can occur at locations that contain antennas. In this diagram, the lone antenna with frequency capital A creates no antinodes but has a lowercase-a-frequency antinode at its location:
# ..........
# ...#......
# #.........
# ....a.....
# ........a.
# .....a....
# ..#.......
# ......A...
# ..........
# ..........
# The first example has antennas with two different frequencies, so the antinodes they create look like this, plus an antinode overlapping the topmost A-frequency antenna:
# ......#....#
# ...#....0...
# ....#0....#.
# ..#....0....
# ....0....#..
# .#....A.....
# ...#........
# #......#....
# ........A...
# .........A..
# ..........#.
# ..........#.
# Because the topmost A-frequency antenna overlaps with a 0-frequency antinode, there are 14 total unique locations that contain an antinode within the bounds of the map.
# Calculate the impact of the signal. How many unique locations within the bounds of the map contain an antinode?
# To begin, get your puzzle input.
# Answer:
# You can also [Share] this puzzle.
TEST = """
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"""
PROD = """
.E..........m..0N.........f.......................
........N........P0...............................
.......j..................................F.......
........1j............P........................C..
...........................3..K......f..........E.
...........V...y...0.....................F........
1.......j.....P....y.N.......................F....
....................m...................C.........
..L......P....p..................w.m..............
............E......p..AU........8......f..........
..............C...............w....d..............
j1...............E..........3.........f........w..
.................p...A..........3.................
.................3..p........KU...w..r..F.........
7.........y........8.......................r......
........y..u......K...............................
...1..................8....C...K..................
...........h.......................6..............
......................U.........A.r..t........6...
...........5.........8..c.........................
.................U................t...............
.....L...O...................t.............d......
.........7........................................
......L..H...c.....9....t.................6.......
...........................c.M..................4.
.....R..7...O.....................................
.......................9......................d...
..................................................
.........L..9...R..........................6c.....
..M.....T.5.................................d.....
.......5OR...................T....................
.......D......o.........v...................r.....
...u....o.........5...............................
.......WR.....Y...........................e...4...
T............O......M..................4..a.......
.Y...................M............................
........W..D...............oh............e........
.......7......Do...................A...e.......4..
.W...Y..D........................h...v..........e.
..........V.....9.l.......h.......a.........n..v..
.......................H.....a2...................
..................................................
...V............Y....J..H2................vn......
..............................H2.................n
................V..........l...........k..........
.T..u........................J...ak...............
..................J.....l.........................
.................l................................
......u.........................................n.
......................J..k............2...........
"""
TEST2 = """
..........
..........
..........
....a.....
..........
.....a....
..........
..........
..........
..........
"""
TEST3 = """
T.........
...T......
.T........
..........
..........
..........
..........
..........
..........
..........
"""
from pprint import pprint
x = TEST
x = PROD
x = x.strip()
# Data in differnt formats for quick access
text = x
lines = x.split("\n")
grid = [list(line) for line in lines]
result = 0
rows = len(grid)
cols = len(grid[0])
antinodes = set()
frequencies = {}
for i in range(rows):
for j in range(cols):
if grid[i][j] == ".":
continue
freq = grid[i][j]
if freq not in frequencies:
frequencies[freq] = []
frequencies[freq].append((i, j))
pprint(frequencies)
for key in frequencies:
for i in range(len(frequencies[key])):
for j in range(i + 1, len(frequencies[key])):
x1, y1 = frequencies[key][i]
x2, y2 = frequencies[key][j]
dx = x2 - x1
dy = y2 - y1
x3, y3 = x2, y2
x4, y4 = x1, y1
while True:
added = False
x3, y3 = x3 + dx, y3 + dy
x4, y4 = x4 - dx, y4 - dy
if 0 <= x3 < rows and 0 <= y3 < cols:
added = True
result += 1
antinodes.add((x3, y3))
if 0 <= x4 < rows and 0 <= y4 < cols:
added = True
result += 1
antinodes.add((x4, y4))
if not added:
break
for key in frequencies:
for x, y in frequencies[key]:
antinodes.add((x, y))
# make the grid again
new_grid = [["." for _ in range(cols)] for _ in range(rows)]
for x, y in antinodes:
new_grid[x][y] = "#"
for key in frequencies:
for x, y in frequencies[key]:
new_grid[x][y] = key
for line in new_grid:
print("".join(line))
# print(len(antinodes))
print(*list(sorted(antinodes)), sep="\n")
# print(result)