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
def test_prog_4(self): code = Intcode([1, 1, 1, 4, 99, 5, 6, 0, 99], 1, 1) result = code.run() assert result == 30 assert code.program == [30, 1, 1, 4, 2, 5, 6, 0, 99] assert code.pointer == 8 assert code.terminated
def test_prog_3(self): code = Intcode([2, 4, 4, 5, 99, 0], 4, 4) result = code.run() assert result == 2 assert code.program == [2, 4, 4, 5, 99, 9801] assert code.pointer == 4 assert code.terminated
def test_prog_2(self): code = Intcode([2, 3, 0, 3, 99], 3) result = code.run() assert result == 2 assert code.program == [2, 3, 0, 6, 99] assert code.pointer == 4 assert code.terminated
def test_prog_1(self): code = Intcode([1, 0, 0, 0, 99]) result = code.run() assert result == 2 assert code.program == [2, 0, 0, 0, 99] assert code.pointer == 4 assert code.terminated
def test_prog_2(self): code = Intcode([2, 3, 0, 3, 99], 3) result = code.run() assert result == (2, True) assert code.program == defaultdict(int, enumerate([2, 3, 0, 6, 99])) assert code.pointer == 4 assert code.terminated
def test_prog_4(self): code = Intcode([1, 1, 1, 4, 99, 5, 6, 0, 99], 1, 1) result = code.run() assert result == (30, True) assert code.program == defaultdict( int, enumerate([30, 1, 1, 4, 2, 5, 6, 0, 99])) assert code.pointer == 8 assert code.terminated
def test_prog_3(self): code = Intcode([2, 4, 4, 5, 99, 0], 4, 4) result = code.run() assert result == (2, True) assert code.program == defaultdict(int, enumerate([2, 4, 4, 5, 99, 9801])) assert code.pointer == 4 assert code.terminated
def test_relative_mode1(self): program = [ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ] code = Intcode(program[:], mode='test') assert code.run() == [ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ]
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}')
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 test_relative_mode3(self): program = [104, 1125899906842624, 99] code = Intcode(program[:], mode='run') assert code.run()[0] == 1125899906842624