Implement 2019 day 11

This commit is contained in:
2021-01-24 17:35:42 +01:00
parent dd039e9276
commit 2c767d5996
2 changed files with 61 additions and 1 deletions

View File

@@ -48,7 +48,7 @@ def part2(data: TextIO) -> int:
dy = ys - cy dy = ys - cy
angles = numpy.arctan2(dy, dx) angles = numpy.arctan2(dy, dx)
distances = numpy.abs(numpy.copy(dx)) + numpy.abs(numpy.copy(dy)) distances = numpy.abs(dx) + numpy.abs(dy)
to_shoot = defaultdict(list) to_shoot = defaultdict(list)

60
2019/aoc2019/day11.py Normal file
View File

@@ -0,0 +1,60 @@
from typing import Dict, Optional, TextIO
from aoc2019.intcode import Computer, read_program
def run_robot(data: TextIO, painted: Optional[Dict[complex, int]] = None) -> Dict[complex, int]:
if painted is None:
painted = {}
computer = Computer(read_program(data))
pos = 0j
direction = 1j
finished = False
while not finished:
try:
computer.run()
finished = True
except IndexError:
pass
while len(computer.output) >= 2:
paint = computer.output.popleft()
turn = computer.output.popleft()
painted[pos] = paint
if turn:
direction *= -1j
else:
direction *= 1j
pos += direction
computer.input.append(painted.get(pos, 0))
return painted
def part1(data: TextIO) -> int:
return len(run_robot(data))
def part2(data: TextIO) -> str:
painted = run_robot(data, {0j: 1})
xmin = int(min(pos.real for pos in painted.keys()))
xmax = int(max(pos.real for pos in painted.keys()))
ymin = int(min(pos.imag for pos in painted.keys()))
ymax = int(max(pos.imag for pos in painted.keys()))
image = ''
for y in reversed(range(ymin, ymax + 1)):
line = ''.join('#' if painted.get(x + y * 1j) == 1 else ' ' for x in range(xmin, xmax + 1))
image += f'{line}\n'
return image[:-1]