aoc2019/day11.py
2021-12-27 17:31:33 +01:00

67 lines
1.7 KiB
Python

from intcode import IntCode
from tools.aoc import AOCDay
from tools.coordinate import Coordinate
from tools.grid import Grid, GridTransformation
from typing import Any
MOVEMENTS = [Coordinate(0, -1), Coordinate(1, 0), Coordinate(0, 1), Coordinate(-1, 0)]
class Day(AOCDay):
inputs = [
[
(2373, "input11")
],
[
("see image above", "input11")
]
]
def part1(self) -> Any:
comp = IntCode(self.getInputAsArraySplit(",", int))
comp.start()
hull = Grid()
face = 0
pos = Coordinate(0, 0)
painted = {}
while not comp.isHalted():
comp.addInput(hull.isSet(pos))
hull.set(pos, comp.getOutput())
painted[pos] = True
face += 1 if comp.getOutput() else -1
pos = pos + MOVEMENTS[face % 4]
return len(painted)
def part2(self) -> Any:
comp = IntCode(self.getInputAsArraySplit(",", int))
comp.start()
hull = Grid()
face = 0
pos = Coordinate(0, 0)
hull.set(pos, True)
while not comp.isHalted():
comp.addInput(hull.isSet(pos))
hull.set(pos, comp.getOutput())
face += 1 if comp.getOutput() else -1
pos = pos + MOVEMENTS[face % 4]
hull.transform(GridTransformation.ROTATE_RIGHT)
hull.transform(GridTransformation.FLIP_HORIZONTALLY)
for x in range(hull.minX, hull.maxX + 1):
for y in range(hull.minY, hull.maxY + 1):
print("#" if hull.isSet(Coordinate(x, y)) else " ", end="")
print()
return "see image above"
if __name__ == '__main__':
day = Day(2019, 11)
day.run(verbose=True)