···22import fileinput
33from collections import deque, defaultdict
4455-66-def dist(x, y):
77- return (((x[0] - y[0]) ** 2) + ((x[1] - y[1]) ** 2)) ** 0.5
88-99-1010-def angle(a, b):
1111- return math.atan2(a[1] - b[1], a[0] - b[0])
55+from utils import Point
126137148# Read problem input
···1711for y, line in enumerate(fileinput.input()):
1812 for x, c in enumerate(line.strip()):
1913 if c == '#':
2020- asteroids.add((x, y))
1414+ asteroids.add(Point(x, y))
211522162317# Part 1
2418detections = {
2525- ast: len(set(angle(ast, other) for other in (asteroids - set(ast))))
2626- for ast in asteroids
1919+ a: len(set(a.angle(o) for o in (asteroids - set([a]))))
2020+ for a in asteroids
2721}
28222923station, num = max(detections.items(), key=lambda x: x[1])
···3832# the station (going from furthest to closest). We rotate in the
3933# positive-radian direction as a hack, since the y-axis coordinates
4034# actually increase going downwards (as opposed to upwards).
4141-by_angles = defaultdict(list)
3535+by_angle = defaultdict(list)
4236for a in asteroids:
4343- by_angles[angle(station, a)].append(a)
3737+ by_angle[station.angle(a)].append(a)
44384545-for k, v in by_angles.items():
4646- v.sort(key=lambda a: -dist(station, a))
3939+for k, v in by_angle.items():
4040+ v.sort(key=station.dist, reverse=True)
47414848-queue = deque(sorted(by_angles.items()))
4242+queue = deque(sorted(by_angle.items()))
49435044# Rotate the queue to the starting angle
5145start = math.pi / 2
···6256 else:
6357 queue.rotate(-1)
64586565-print "200th asteroid checksum:", vaporized[0] * 100 + vaporized[1]
5959+print "200th asteroid checksum:", vaporized.x * 100 + vaporized.y