<gh_stars>0
#!/usr/bin/env python3

import math


def angle(x, y):
    theta = math.atan2(y, x)
    # Change to angle from -Y axis
    theta += 2 * math.pi if theta < -math.pi / 2 else 0
    return round(theta, 5)


class Asteroid:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.angles = {}

    def add(self, other):
        theta = angle(other.x - self.x, other.y - self.y)
        store = self.angles.setdefault(theta, set())
        store.add(other)

    def dist(self, other):
        return (other.x - self.x)**2 + (other.y - self.y)**2

    def __hash__(self):
        return hash((self.x, self.y))


with open("inputs/10") as f:
    rows = [l.strip() for l in f]

asteroids = set()
for r in range(len(rows)):
    for c in range(len(rows[r])):
        if rows[r][c] == "#":
            new = Asteroid(c, r)
            for a in asteroids:
                new.add(a)
                a.add(new)
            asteroids.add(new)

best = max(asteroids, key=lambda p: len(p.angles))
print(f"{len(best.angles)} at {best.x, best.y}")  # Part 1

for angle in best.angles:
    best.angles[angle] = sorted(best.angles[angle], key=best.dist)
sorted_asteroids = [a[1] for a in sorted(best.angles.items())]

depth = 0
zapped = 0
while zapped < 200:
    for asteroids in sorted_asteroids:
        if len(asteroids) <= depth:
            continue
        zapped += 1
        if zapped == 200:
            target = asteroids[depth]
            print(target.x * 100 + target.y)
            break
    depth += 1
