2019-12-15 23:34:38 +01:00
|
|
|
from collections import defaultdict
|
|
|
|
import time
|
2019-12-09 18:27:55 +01:00
|
|
|
|
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-15 23:34:38 +01:00
|
|
|
EX = [
|
|
|
|
".#....#####...#..",
|
|
|
|
"##...##.#####..##",
|
|
|
|
"##...#...#.#####.",
|
|
|
|
"..#.....#...###..",
|
|
|
|
"..#.#.....#....##",
|
|
|
|
]
|
|
|
|
|
2019-12-15 18:42:55 +01:00
|
|
|
|
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 23:34:38 +01:00
|
|
|
# amap = [line.rstrip() for line in EX]
|
2019-12-09 18:27:55 +01:00
|
|
|
|
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
|
|
|
|
2019-12-15 23:34:38 +01:00
|
|
|
# Part 2
|
|
|
|
pos = best_pos
|
|
|
|
asteroids.remove(pos)
|
|
|
|
destroyed = 0
|
|
|
|
angles = defaultdict(list)
|
|
|
|
for a in asteroids:
|
|
|
|
angle = np.angle(np.complex(a[0] - pos[0], a[1] - pos[1]), deg=True)
|
|
|
|
angles[angle].append(a)
|
|
|
|
current_angle = -90.0
|
|
|
|
while asteroids:
|
|
|
|
print(f"Laser at {pos}.")
|
|
|
|
print(f"Current angle: {current_angle}.")
|
|
|
|
targets = angles[current_angle]
|
|
|
|
try:
|
|
|
|
next_angle = min([a for a in angles.keys() if a > current_angle])
|
|
|
|
except ValueError:
|
|
|
|
next_angle = -180.0
|
|
|
|
if not targets:
|
|
|
|
current_angle = next_angle
|
|
|
|
continue
|
|
|
|
print(f"Acquired targets {targets}.")
|
|
|
|
nearest_target = min(targets, key=lambda t: dist_idea(pos, t))
|
|
|
|
print(f"Nearest target: {nearest_target}.")
|
|
|
|
|
|
|
|
# Pew!
|
|
|
|
asteroids.remove(nearest_target)
|
|
|
|
targets.remove(nearest_target)
|
|
|
|
|
|
|
|
destroyed += 1
|
|
|
|
print(f"Destroyed {nearest_target} -- total of {destroyed} asteroids.")
|
|
|
|
if destroyed == 200:
|
|
|
|
print(f"Destroyed 200th asteroid at {nearest_target}.")
|
|
|
|
break
|
|
|
|
|
|
|
|
current_angle = next_angle
|
|
|
|
|
|
|
|
draw(asteroids, pos, [nearest_target])
|
|
|
|
time.sleep(0.2)
|
|
|
|
|
|
|
|
print(f"Answer: {100 * nearest_target[0] + nearest_target[1]}.")
|
|
|
|
|
|
|
|
def draw(asteroids, station_pos, specials=[]):
|
2019-12-15 18:42:55 +01:00
|
|
|
for y in range(DIM):
|
|
|
|
print(str(y).zfill(2), end=" ")
|
|
|
|
for x in range(DIM):
|
|
|
|
if (x, y) == station_pos:
|
|
|
|
print("@", end="")
|
2019-12-15 23:34:38 +01:00
|
|
|
elif (x, y) in specials:
|
|
|
|
print("X", end="")
|
2019-12-15 18:42:55 +01:00
|
|
|
elif (x, y) in asteroids:
|
|
|
|
print("█", end="")
|
|
|
|
else:
|
|
|
|
print(" ", end="")
|
|
|
|
print()
|
2019-12-15 23:34:38 +01:00
|
|
|
print(" " + "0123456789"*4)
|
2019-12-09 18:27:55 +01:00
|
|
|
|
2019-12-15 23:34:38 +01:00
|
|
|
def num_visible_asts(asteroids, ref):
|
|
|
|
asteroids = asteroids.copy()
|
|
|
|
asteroids.remove(ref)
|
|
|
|
angles = [np.angle(np.complex(a[0] - ref[0], a[1] - ref[1])) for a in asteroids]
|
2019-12-15 18:42:31 +01:00
|
|
|
return len(set(angles))
|
2019-12-09 18:27:55 +01:00
|
|
|
|
2019-12-15 23:34:38 +01:00
|
|
|
def dist_idea(a, b):
|
|
|
|
return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2
|
|
|
|
|
2019-12-09 18:27:55 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|