AdventOfCode/2019/day10.py

51 lines
1.2 KiB
Python
Raw Normal View History

2019-12-09 18:27:55 +01:00
import math
2019-12-15 18:42:31 +01:00
import numpy as np
2019-12-09 18:27:55 +01:00
2019-12-15 18:42:55 +01:00
DIM = 40
2019-12-09 18:27:55 +01:00
def main():
with open("day10.txt", "rt") as input_file:
amap = [line.rstrip() for line in input_file.readlines()]
2019-12-15 18:42:31 +01:00
asteroids = []
for y, line in enumerate(amap):
for x, value in enumerate(line):
2019-12-09 18:27:55 +01:00
if value == "#":
2019-12-15 18:42:31 +01:00
asteroids.append((x, y))
2019-12-09 18:27:55 +01:00
2019-12-15 18:42:55 +01:00
# Part 1
2019-12-09 18:27:55 +01:00
best_vis = 0
2019-12-15 18:42:31 +01:00
best_pos = None
for pos in asteroids:
vis = num_visible_asts(asteroids, pos)
if vis > best_vis:
best_vis, best_pos = vis, pos
print(f"Best visibility is {best_vis} at {best_pos}.")
2019-12-15 18:42:55 +01:00
draw(asteroids, best_pos)
def draw(asteroids, station_pos):
for y in range(DIM):
print(str(y).zfill(2), end=" ")
for x in range(DIM):
if (x, y) == station_pos:
print("@", end="")
elif (x, y) in asteroids:
print("", end="")
else:
print(" ", end="")
print()
print(" " + "0123456789"*4)
2019-12-09 18:27:55 +01:00
def num_visible_asts(asts, ref):
2019-12-15 18:42:31 +01:00
asts = asts.copy()
asts.remove(ref)
angles = [np.angle(np.complex(ast[0] - ref[0], ast[1] - ref[1])) for ast in asts]
return len(set(angles))
2019-12-09 18:27:55 +01:00
if __name__ == "__main__":
main()