My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Update day10.py

+10 -16
+10 -16
2019/day10.py
··· 2 2 import fileinput 3 3 from collections import deque, defaultdict 4 4 5 - 6 - def dist(x, y): 7 - return (((x[0] - y[0]) ** 2) + ((x[1] - y[1]) ** 2)) ** 0.5 8 - 9 - 10 - def angle(a, b): 11 - return math.atan2(a[1] - b[1], a[0] - b[0]) 5 + from utils import Point 12 6 13 7 14 8 # Read problem input ··· 17 11 for y, line in enumerate(fileinput.input()): 18 12 for x, c in enumerate(line.strip()): 19 13 if c == '#': 20 - asteroids.add((x, y)) 14 + asteroids.add(Point(x, y)) 21 15 22 16 23 17 # Part 1 24 18 detections = { 25 - ast: len(set(angle(ast, other) for other in (asteroids - set(ast)))) 26 - for ast in asteroids 19 + a: len(set(a.angle(o) for o in (asteroids - set([a])))) 20 + for a in asteroids 27 21 } 28 22 29 23 station, num = max(detections.items(), key=lambda x: x[1]) ··· 38 32 # the station (going from furthest to closest). We rotate in the 39 33 # positive-radian direction as a hack, since the y-axis coordinates 40 34 # actually increase going downwards (as opposed to upwards). 41 - by_angles = defaultdict(list) 35 + by_angle = defaultdict(list) 42 36 for a in asteroids: 43 - by_angles[angle(station, a)].append(a) 37 + by_angle[station.angle(a)].append(a) 44 38 45 - for k, v in by_angles.items(): 46 - v.sort(key=lambda a: -dist(station, a)) 39 + for k, v in by_angle.items(): 40 + v.sort(key=station.dist, reverse=True) 47 41 48 - queue = deque(sorted(by_angles.items())) 42 + queue = deque(sorted(by_angle.items())) 49 43 50 44 # Rotate the queue to the starting angle 51 45 start = math.pi / 2 ··· 62 56 else: 63 57 queue.rotate(-1) 64 58 65 - print "200th asteroid checksum:", vaporized[0] * 100 + vaporized[1] 59 + print "200th asteroid checksum:", vaporized.x * 100 + vaporized.y