mirror of
https://github.com/thib8956/advent-of-code.git
synced 2026-02-27 10:48:14 +00:00
- Move intcode/ from 2019/ folder to the root of the repository - The package is automatically installed with the adventofcode package with `pip install -e .` command
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
from adventofcode.intcode import interpret_intcode, Interpreter
|
|
|
|
|
|
def paint(program, initial_state):
|
|
interpreter = Interpreter(program)
|
|
pos = 0 + 0j
|
|
direction = 0 - 1j # initially facing up ^
|
|
colors = {}
|
|
colors[pos] = initial_state
|
|
while True:
|
|
interpreter.stdin.append(colors.get(pos, 0))
|
|
interpreter.interpret(break_on_output=True)
|
|
interpreter.interpret(break_on_output=True)
|
|
if interpreter.halted:
|
|
return colors
|
|
|
|
color = interpreter.stdout.pop(0)
|
|
colors[pos] = color
|
|
|
|
turn = interpreter.stdout.pop(0)
|
|
if turn == 0:
|
|
direction *= -1j # turn left
|
|
elif turn == 1:
|
|
direction *= 1j # turn right
|
|
else:
|
|
assert False
|
|
|
|
pos += direction
|
|
|
|
|
|
def main(data, part=1):
|
|
program = list(map(int, data.readline().rstrip().split(",")))
|
|
if part == 1:
|
|
colors = paint(program, initial_state=0)
|
|
print("Part 1: ", len(colors))
|
|
else:
|
|
colors = paint(program, initial_state=1)
|
|
part2(colors)
|
|
|
|
|
|
def part2(colors):
|
|
min_x = int(min(x.real for x in colors.keys()))
|
|
max_x = int(max(x.real for x in colors.keys()))
|
|
min_y = int(min(x.imag for x in colors.keys()))
|
|
max_y = int(max(x.imag for x in colors.keys()))
|
|
for y in range(min_y, max_y + 1):
|
|
l = []
|
|
for x in range(min_x, max_x + 1):
|
|
point = complex(x, y)
|
|
l.append("\u2588" if point in colors and colors[point] == 1 else " ")
|
|
print("".join(l))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import fileinput
|
|
with fileinput.input() as f:
|
|
main(f, part=1)
|
|
#with fileinput.input() as f:
|
|
#main(f, part=2) # FIXME unable to run both parts simultaneously
|
|
|