diff --git a/2019/day10-asteroids.py b/2019/day10-asteroids.py new file mode 100644 index 0000000..fe237ba --- /dev/null +++ b/2019/day10-asteroids.py @@ -0,0 +1,73 @@ +from dataclasses import dataclass +from typing import Tuple, Set + + +@dataclass +class Asteroid: + x: int + y: int + + def __hash__(self): + return hash((self.x, self.y)) + + @staticmethod + def square_length(a: "Asteroid", b: "Asteroid"): + return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y) + + @staticmethod + def cross_product(a: "Asteroid", b: "Asteroid", c: "Asteroid"): + return (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y) + + @staticmethod + def dot_product(a: "Asteroid", b: "Asteroid", c: "Asteroid"): + return (c.x - a.x) * (b.x - a.x) + (c.y - a.y) * (b.y - a.y) + + @staticmethod + def is_between(a: "Asteroid", b: "Asteroid", c: "Asteroid"): + """Check if c is between a and b.""" + cross_product = Asteroid.cross_product(a, b, c) + + # compare versus epsilon for floating point values, or != 0 if using integers + if abs(cross_product) != 0: + return False + + dot_product = Asteroid.dot_product(a, b, c) + if dot_product < 0: + return False + + squared_length_ba = Asteroid.square_length(a, b) + if dot_product > squared_length_ba: + return False + + return True + + def can_see(self, other: "Asteroid", asteroids: Set["Asteroid"]) -> bool: + for asteroid in asteroids: + if asteroid in [self, other]: + continue + if Asteroid.is_between(self, other, asteroid): + return False + return True + + +def main(): + asteroids = set() + with open("inputs/day10") as f: + for y, line in enumerate(f): + for x, pixel in enumerate(line): + if pixel == "#": + asteroids.add(Asteroid(x, y)) + + max_visible = 0 + for source in asteroids: + visible = 0 + for destination in asteroids: + if destination != source and source.can_see(destination, asteroids): + visible += 1 + if visible > max_visible: + max_visible = visible + print(max_visible) + + +if __name__ == "__main__": + main() diff --git a/2019/inputs/day10 b/2019/inputs/day10 new file mode 100644 index 0000000..eea0a46 --- /dev/null +++ b/2019/inputs/day10 @@ -0,0 +1,25 @@ +#..#.#.#.######..#.#...## +##.#..#.#..##.#..######.# +.#.##.#..##..#.#.####.#.. +.#..##.#.#..#.#...#...#.# +#...###.##.##..##...#..#. +##..#.#.#.###...#.##..#.# +###.###.#.##.##....#####. +.#####.#.#...#..#####..#. +.#.##...#.#...#####.##... +######.#..##.#..#.#.#.... +###.##.#######....##.#..# +.####.##..#.##.#.#.##...# +##...##.######..##..#.### +...###...#..#...#.###..#. +.#####...##..#..#####.### +.#####..#.#######.###.##. +#...###.####.##.##.#.##.# +.#.#.#.#.#.##.#..#.#..### +##.#.####.###....###..##. +#..##.#....#..#..#.#..#.# +##..#..#...#..##..####..# +....#.....##..#.##.#...## +.##..#.#..##..##.#..##..# +.##..#####....#####.#.#.# +#..#..#..##...#..#.#.#.## \ No newline at end of file diff --git a/2019/inputs/day10-ex1 b/2019/inputs/day10-ex1 new file mode 100644 index 0000000..8650346 --- /dev/null +++ b/2019/inputs/day10-ex1 @@ -0,0 +1,10 @@ +......#.#. +#..#.#.... +..#######. +.#.#.###.. +.#..#..... +..#....#.# +#..#....#. +.##.#..### +##...#..#. +.#....#### \ No newline at end of file diff --git a/2019/inputs/day10-ex2 b/2019/inputs/day10-ex2 new file mode 100644 index 0000000..dc708ab --- /dev/null +++ b/2019/inputs/day10-ex2 @@ -0,0 +1,10 @@ +#.#...#.#. +.###....#. +.#....#... +##.#.#.#.# +....#.#.#. +.##..###.# +..#...##.. +..##....## +......#... +.####.###. \ No newline at end of file diff --git a/2019/inputs/day10-ex3 b/2019/inputs/day10-ex3 new file mode 100644 index 0000000..cb09bb7 --- /dev/null +++ b/2019/inputs/day10-ex3 @@ -0,0 +1,10 @@ +.#..#..### +####.###.# +....###.#. +..###.##.# +##.##.#.#. +....###..# +..#.#..#.# +#..#.#.### +.##...##.# +.....#.#.. \ No newline at end of file diff --git a/2019/inputs/day10-ex4 b/2019/inputs/day10-ex4 new file mode 100644 index 0000000..8380323 --- /dev/null +++ b/2019/inputs/day10-ex4 @@ -0,0 +1,20 @@ +.#..##.###...####### +##.############..##. +.#.######.########.# +.###.#######.####.#. +#####.##.#.##.###.## +..#####..#.######### +#################### +#.####....###.#.#.## +##.################# +#####.##.###..####.. +..######..##.####### +####.##.####...##..# +.#####..#.######.### +##...#.##########... +#.##########.####### +.####.#.###.###.#.## +....##.##.###..##### +.#.#.###########.### +#.#.#.#####.####.### +###.##.####.##.#..## \ No newline at end of file