grid = {(0, 0): 1} paint_bot = PaintBot(Point(0, 0), 90) def input_func(): return grid.get(paint_bot.pos.tupleify(), 0) output = [] def output_func(val): global output output.append(val) if len(output) == 2: grid[paint_bot.pos.tupleify()] = output[0] paint_bot.rotate(output[1]) paint_bot.move() output = [] computer = IntcodeComputer(intcode, input_func, output_func) computer.run() for y in range(paint_bot.max_up + 2, paint_bot.max_down - 2, -1): line = "" for x in range(paint_bot.max_left - 2, paint_bot.max_right + 2): val = grid.get((x, y), 0) line += " " if val == 0 else "█" print(line)
for original_direction, expected_xy, expected_dir in \ zip([(0, 1), (1, 0), (-1, 0), (0, -1)], [(4, 5), (5, 6), (5, 4), (6, 5)], [(-1, 0), (0, 1), (0, -1), (1, 0)]): assert (turn(5, 5, original_direction, 1)[1] == expected_dir) assert (turn(5, 5, original_direction, 1)[0] == expected_xy) if __name__ == "__main__": painted_pixels = {} # {(x, y): color} x_ = 0 y_ = 0 dir_ = (0, -1) start = True int_computer = IntcodeComputer(deque(), deque(), code=code) while int_computer.op != '99': if start: color = WHITE start = False else: color = painted_pixels.get((x_, y_), BLACK) int_computer.in_que = deque([color]) while len(int_computer.out_que) != 2: int_computer.run_prog(break_on_in=True, break_on_out=True) if int_computer.op == '99': break elif int_computer.op == '03' and len(int_computer.in_que) == 0: int_computer.in_que.append(color) if int_computer.op != '99':