From 05a62c5eb49a99f2f604b610591246fed79443c0 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sun, 3 Aug 2025 20:37:54 +0200 Subject: [PATCH] 2019 day11 --- 2019/day11/day11.py | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2019/day9/day9.py | 2 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 2019/day11/day11.py diff --git a/2019/day11/day11.py b/2019/day11/day11.py new file mode 100644 index 0000000..77257cb --- /dev/null +++ b/2019/day11/day11.py @@ -0,0 +1,65 @@ +# TODO replace PYTHONPATH hack with a proper solution, like making intcode an +# installed module https://stackoverflow.com/a/50194143 +import sys +from pathlib import Path +sys.path.append(str(Path(__file__).absolute().parent.parent / "intcode")) +from 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 + diff --git a/2019/day9/day9.py b/2019/day9/day9.py index 6fbcab5..037beb5 100644 --- a/2019/day9/day9.py +++ b/2019/day9/day9.py @@ -5,7 +5,7 @@ from pathlib import Path sys.path.append(str(Path(__file__).absolute().parent.parent / "intcode")) -from intcode import interpret_intcode, Interpreter +from intcode import interpret_intcode def main(inp):