def run_turtle(initial, program): white_tiles = initial painted_tiles = set() current_pos = (0, 0) directionPointer = 0 def input_function(): return 1 if (current_pos in white_tiles) else 0 turtle = IntcodeComputer(program, input_function) while not turtle.stopped: painted_tiles.add(current_pos) colour = turtle.get_next_output() if colour == 0: try: white_tiles.remove(current_pos) except KeyError: pass if colour == 1: white_tiles.add(current_pos) turn = turtle.get_next_output() if turn == 0: directionPointer = (directionPointer - 1) % 4 if turn == 1: directionPointer = (directionPointer + 1) % 4 direction = DIRECTIONS[directionPointer] current_pos = (current_pos[0] + direction[0], current_pos[1] + direction[1]) return white_tiles, painted_tiles
def part1(data): last_out = 1 direction_pointer = 0 DIRECTIONS = {1: (0, 1), 4: (1, 0), 2: (0, -1), 3: (-1, 0)} walls = set() current_pos = (0, 0) to_input = 1 def turn_right(): table = {1: 4, 2: 3, 3: 1, 4: 2} nonlocal to_input to_input = table[to_input] def turn_left(): table = {1: 3, 2: 4, 3: 2, 4: 1} nonlocal to_input to_input = table[to_input] def inf(): nonlocal to_input return to_input turtle = IntcodeComputer(data, inf) walls.add((current_pos[0], current_pos[1], 3)) while True: turn_left() check_wall = turtle.get_next_output() while check_wall == 0: walls.add((current_pos[0] + DIRECTIONS[to_input][0], current_pos[1] + DIRECTIONS[to_input][1])) turn_right() check_wall = turtle.get_next_output() current_pos = (current_pos[0] + DIRECTIONS[to_input][0], current_pos[1] + DIRECTIONS[to_input][1]) if check_wall == 2: walls.add((current_pos[0], current_pos[1], 2)) if current_pos == (0, 0): break bounds = [0, 0, 0, 0] for wall in walls: if wall[0] < bounds[0]: bounds[0] = wall[0] if wall[0] > bounds[2]: bounds[2] = wall[0] if wall[1] < bounds[1]: bounds[1] = wall[1] if wall[1] > bounds[3]: bounds[3] = wall[1] maze = [[0 for j in range(bounds[2] - bounds[0] + 1)] for i in range(bounds[3] - bounds[1] + 1)] for wall in walls: if len(wall) == 3: maze[wall[1] - bounds[1]][wall[0] - bounds[0]] = wall[2] else: maze[wall[1] - bounds[1]][wall[0] - bounds[0]] = 1 for line in maze: print("".join([str(x) for x in line]).replace("1", "█").replace("0", ' ').replace("2", "E")) return maze
def part2(data): data[0] = 2 ballX = 0 paddleX = 0 def inf(): nonlocal paddleX, ballX if paddleX > ballX: return -1 if paddleX < ballX: return 1 return 0 computer = IntcodeComputer(data, inf) while not computer.stopped: tileData = [] for i in range(3): tileData.append(computer.get_next_output()) if tileData[2] == 3: paddleX = tileData[0] if tileData[2] == 4: ballX = tileData[0] return computer.outputs[-1]
turtle.fillcolor("white" if color else "black") turtle.begin_fill() h = turtle.heading() turtle.setheading(0) for _ in range(4): turtle.forward(FACTOR) turtle.right(90) turtle.setheading(h) turtle.end_fill() turtle.penup() while True: comp.set_inputs( [floor.get((turtle.xcor() / FACTOR, turtle.ycor() / FACTOR), 0)]) paint_color = comp.get_next_output() paint_square(turtle, paint_color) floor[(turtle.xcor() / FACTOR, turtle.ycor() / FACTOR)] = paint_color if comp.finished: break rotation = comp.get_next_output() if rotation == 1: turtle.right(90) else: turtle.left(90) turtle.forward(FACTOR) time.sleep(10)