-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
50 lines (41 loc) · 1.4 KB
/
script.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
from GhostyUtils import aoc
from GhostyUtils.grid import Grid
from GhostyUtils.vec2 import Vec2
from collections import defaultdict
def main():
grid = Grid(aoc.read_lines())
asteroids = list(grid.find_all('#'))
most_seen = 0
best_asteroid = None
best_angles = {}
for asteroid in asteroids:
angles = defaultdict(list)
for other in asteroids:
if other == asteroid:
continue
angle = (Vec2(other) - Vec2(asteroid)).north_angle()
angles[angle].append(other)
angles[angle].sort(key=lambda a: Vec2.manhattan_distance(asteroid, a))
if len(angles) > most_seen:
most_seen = len(angles)
best_asteroid = asteroid
best_angles = angles
print('p1:', most_seen, best_asteroid)
angles = sorted(best_angles.keys())
vaporised = 0
last_vaporised = None
empty = []
while vaporised != 200:
for angle in angles:
if best_angles[angle]:
last_vaporised = Vec2(best_angles[angle].pop(0))
vaporised += 1
if vaporised == 200:
break
if not best_angles[angle]:
empty.append(angle)
for angle in empty:
del best_angles[angle]
print('p2:', last_vaporised.x * 100 + last_vaporised.y, last_vaporised)
if __name__ == "__main__":
main()