def painting_robot(inputs: list, start: int) -> (np.array, np.array): grid = np.zeros([50, 65], dtype=int) boolgrid = np.zeros([50, 65], dtype=bool) robot_pos = np.array([0, 20]) robot_dir = 0 directions = np.array([[0, 1], [1, 0], [0, -1], [-1, 0]]) terminated = False paint = Intcode(inputs[:], inp=[start], mode='run') while not terminated: colour, terminated = paint.run() grid[tuple(robot_pos)] = colour boolgrid[tuple(robot_pos)] = True direction, terminated = paint.run() if terminated: break if direction == 0: robot_dir -= 1 elif direction == 1: robot_dir += 1 else: raise ValueError robot_dir %= 4 robot_pos += directions[robot_dir] paint.next_inp(grid[tuple(robot_pos)]) return grid, boolgrid
class Robot(object): dX = {1: 0, 2: 0, 3: -1, 4: 1} dY = {1: 1, 2: -1, 3: 0, 4: 0} back = {1: 2, 2: 1, 3: 4, 4: 3} def __init__(self, code): self.x = 21 self.y = 19 self.code = code self.program = Intcode(self.code, inp=[], mode='run') self.stack = [] self.found = [] self.grid = np.full([41, 41], -1, dtype=int) self.grid[self.x, self.y] = 1 self.explore() def step(self, direction): self.program.next_inp(direction) result = self.program.run()[0] if result: self.x += Robot.dX[direction] self.y += Robot.dY[direction] return result def explore(self): for direction in range(1, 5): if self.grid[self.x + Robot.dX[direction], self.y + Robot.dY[direction]] == -1: result = self.step(direction) if result == 2: self.found = [self.x, self.y] if result: self.grid[self.x, self.y] = 1 self.step(Robot.back[direction]) self.stack.append(["N", direction]) else: self.grid[self.x + Robot.dX[direction], self.y + Robot.dY[direction]] = 0 def move(self): while self.stack: move_type, direction = self.stack.pop() self.step(direction) if move_type == "N": self.stack.append(["R", Robot.back[direction]]) self.explore()
def main(): inputs = list(map(int, [code.strip().split(',') for code in open('../inputs/Advent2019_13.txt', 'r')][0])) terminated = False game = Intcode(inputs[:], mode='run') count = 0 while not terminated: game.run() game.run() draw, terminated = game.run() if draw == 2: count += 1 print(f'AoC 2019 Day 12, Part 1: Number of blocks is {count}') terminated = False game = Intcode(inputs[:], inp=[0], mode='run') game.program[0] = 2 # Insert 2 quarters paddle = False while not terminated: draw_x, terminated = game.run() draw_y, terminated = game.run() draw, terminated = game.run() if draw_x == -1 and draw_y == 0: current_score = draw continue if draw == 3: paddle = draw_x if draw == 4: ball = draw_x if paddle: if paddle < ball: game.next_inp(1) elif paddle > ball: game.next_inp(-1) else: game.next_inp(0) print(f'AoC 2019 Day 12, Part 2: High Score {current_score}')