Solve 2019:10 "Monitoring Station".

This commit is contained in:
Anders Englöf Ytterström 2023-11-27 16:33:09 +01:00 committed by Anders Englöf Ytterström
parent d9142068ea
commit 262ad34c51
4 changed files with 88 additions and 4 deletions

View file

@ -3,7 +3,7 @@ import sys
year = 2019
try:
_, day_no, name = sys.argv
_, day_no, *name = sys.argv
except ValueError:
day_no = None
name = None
@ -13,6 +13,7 @@ print(
)
if day_no and name:
name = " ".join(name)
padded_no = day_no.zfill(2)
print(f"\n- creating output/day_{padded_no}.py")
with open("output/day_{}.py".format(padded_no), "w") as s:

View file

@ -15,7 +15,7 @@ def parse_input(data):
@answer(
1,
"[intcode 0.3.0] Given the phase settings [0, 3, 1, 2, 4], the highest achievable signal to the thruster is {}",
"[intcode 0.3.0] The highest achievable signal to the thruster is {}",
)
def part_1(program):
thruster_signals = []
@ -30,7 +30,7 @@ def part_1(program):
@answer(
2,
"[intcode 0.3.0] Given the phase settings [7, 8, 5, 9, 6] and creating feedback loop, the highest achievable signal to the thruster is {}",
"[intcode 0.3.0] By creating a feedback loop, the highest achievable signal to the thruster is {}",
)
def part_2(program):
thruster_signals = []

View file

@ -11,7 +11,7 @@ def parse_input(data):
return data
@answer(1, "the product of all 1s and 2s in the layer with fewest 0s is {}")
@answer(1, "The product of all 1s and 2s in the layer with fewest 0s is {}")
def part_1(data):
layers = sorted(map(Counter, wrap(data, 25 * 6)), key=lambda c: c["0"])
a = layers[0]["1"]

View file

@ -0,0 +1,83 @@
from collections import OrderedDict, defaultdict, deque
from math import atan2
from output import answer, puzzleinput
n = 10
title = "Monitoring Station"
@puzzleinput(n)
def parse_input(data):
return data.strip().split()
@answer(1, "The monitor station will see {} asteroids at best")
def part_1(matrix):
_pos, visible = _map_visible_asteroids(matrix)
return len(set(dict(visible).values()))
@answer(
2,
"The asteroid at y=3 x=17 (checksum {}) will be the 200th lazer vapored asteroid, making some elf happy",
)
def part_2(matrix):
pos, visible = _map_visible_asteroids(matrix)
targets_upper = defaultdict(list)
targets_lower = defaultdict(list)
targets = dict()
for xy, angle in visible:
if angle < 0:
targets_lower[angle].append(xy)
else:
targets_upper[angle].append(xy)
for k, v in OrderedDict(
sorted(targets_upper.items(), key=lambda x: x[0], reverse=True)
+ sorted(targets_lower.items(), key=lambda x: x[0], reverse=True)
).items():
targets[k] = deque(
sorted(
v,
key=lambda xy: sum(abs(pos[i] - xy[i]) for i in range(2)),
)
)
vapored = 0
x = 0
y = 0
while vapored < 200:
popped = False
for tk in targets.keys():
if targets[tk]:
x, y = targets[tk].pop()
vapored += 1
popped = True
if vapored == 200:
break
if not popped:
break
return x * 100 + y
def _map_visible_asteroids(matrix):
asteroids = []
visible = defaultdict(int)
for y in range(len(matrix)):
for x in range(len(matrix[0])):
if matrix[y][x] == "#":
asteroids.append((x, y))
for a, b in asteroids:
visible[(a, b)] = [
((x, y), atan2(x - a, y - b)) for x, y in asteroids if (a, b) != (x, y)
]
return max(visible.items(), key=lambda x: len(set(dict(x[1]).values())))
if __name__ == "__main__":
parsed = parse_input()
part_1(parsed)
part_2(parsed)