mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-11-22 06:28:11 +01:00
Add day10
This commit is contained in:
parent
ec29b13c5f
commit
5cffcf875c
6 changed files with 148 additions and 0 deletions
73
2019/day10-asteroids.py
Normal file
73
2019/day10-asteroids.py
Normal file
|
@ -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()
|
25
2019/inputs/day10
Normal file
25
2019/inputs/day10
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#..#.#.#.######..#.#...##
|
||||||
|
##.#..#.#..##.#..######.#
|
||||||
|
.#.##.#..##..#.#.####.#..
|
||||||
|
.#..##.#.#..#.#...#...#.#
|
||||||
|
#...###.##.##..##...#..#.
|
||||||
|
##..#.#.#.###...#.##..#.#
|
||||||
|
###.###.#.##.##....#####.
|
||||||
|
.#####.#.#...#..#####..#.
|
||||||
|
.#.##...#.#...#####.##...
|
||||||
|
######.#..##.#..#.#.#....
|
||||||
|
###.##.#######....##.#..#
|
||||||
|
.####.##..#.##.#.#.##...#
|
||||||
|
##...##.######..##..#.###
|
||||||
|
...###...#..#...#.###..#.
|
||||||
|
.#####...##..#..#####.###
|
||||||
|
.#####..#.#######.###.##.
|
||||||
|
#...###.####.##.##.#.##.#
|
||||||
|
.#.#.#.#.#.##.#..#.#..###
|
||||||
|
##.#.####.###....###..##.
|
||||||
|
#..##.#....#..#..#.#..#.#
|
||||||
|
##..#..#...#..##..####..#
|
||||||
|
....#.....##..#.##.#...##
|
||||||
|
.##..#.#..##..##.#..##..#
|
||||||
|
.##..#####....#####.#.#.#
|
||||||
|
#..#..#..##...#..#.#.#.##
|
10
2019/inputs/day10-ex1
Normal file
10
2019/inputs/day10-ex1
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
......#.#.
|
||||||
|
#..#.#....
|
||||||
|
..#######.
|
||||||
|
.#.#.###..
|
||||||
|
.#..#.....
|
||||||
|
..#....#.#
|
||||||
|
#..#....#.
|
||||||
|
.##.#..###
|
||||||
|
##...#..#.
|
||||||
|
.#....####
|
10
2019/inputs/day10-ex2
Normal file
10
2019/inputs/day10-ex2
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#.#...#.#.
|
||||||
|
.###....#.
|
||||||
|
.#....#...
|
||||||
|
##.#.#.#.#
|
||||||
|
....#.#.#.
|
||||||
|
.##..###.#
|
||||||
|
..#...##..
|
||||||
|
..##....##
|
||||||
|
......#...
|
||||||
|
.####.###.
|
10
2019/inputs/day10-ex3
Normal file
10
2019/inputs/day10-ex3
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
.#..#..###
|
||||||
|
####.###.#
|
||||||
|
....###.#.
|
||||||
|
..###.##.#
|
||||||
|
##.##.#.#.
|
||||||
|
....###..#
|
||||||
|
..#.#..#.#
|
||||||
|
#..#.#.###
|
||||||
|
.##...##.#
|
||||||
|
.....#.#..
|
20
2019/inputs/day10-ex4
Normal file
20
2019/inputs/day10-ex4
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
.#..##.###...#######
|
||||||
|
##.############..##.
|
||||||
|
.#.######.########.#
|
||||||
|
.###.#######.####.#.
|
||||||
|
#####.##.#.##.###.##
|
||||||
|
..#####..#.#########
|
||||||
|
####################
|
||||||
|
#.####....###.#.#.##
|
||||||
|
##.#################
|
||||||
|
#####.##.###..####..
|
||||||
|
..######..##.#######
|
||||||
|
####.##.####...##..#
|
||||||
|
.#####..#.######.###
|
||||||
|
##...#.##########...
|
||||||
|
#.##########.#######
|
||||||
|
.####.#.###.###.#.##
|
||||||
|
....##.##.###..#####
|
||||||
|
.#.#.###########.###
|
||||||
|
#.#.#.#####.####.###
|
||||||
|
###.##.####.##.#..##
|
Loading…
Reference in a new issue