def test_examples(self): program = read_program('day09/test1.txt') self.assertEqual(list(run_program(program).out), program) program = read_program('day09/test2.txt') self.assertEqual(len(str(run_program(program).out[0])), 16) program = read_program('day09/test3.txt') self.assertEqual(run_program(program).out[0], 1125899906842624)
def test_run_program(self): program = d.read_program('day05/test1.txt') c = d.run_program(program) self.assertEqual(c.ip, 4) self.assertEqual(c.ram, {k: v for k, v in enumerate([1002, 4, 3, 4, 99])}) program = d.read_program('day05/test2.txt') c = d.run_program(program) self.assertEqual(c.ip, 4) self.assertEqual(c.ram, {k: v for k, v in enumerate([1101, 100, -1, 4, 99])})
def test_jump(self): program = d.read_program('day05/test7.txt') c = d.run_program(program, 0) self.assertEqual(c.out[0], 0) program = d.read_program('day05/test7.txt') c = d.run_program(program, -1) self.assertEqual(c.out[0], 1) program = d.read_program('day05/test8.txt') c = d.run_program(program, 0) self.assertEqual(c.out[0], 0) program = d.read_program('day05/test8.txt') c = d.run_program(program, 1000) self.assertEqual(c.out[0], 1)
def test_compare(self): program = d.read_program('day05/test3.txt') c = d.run_program(program, 8) self.assertEqual(c.out[0], 1) program = d.read_program('day05/test3.txt') c = d.run_program(program, 80) self.assertEqual(c.out[0], 0) program = d.read_program('day05/test4.txt') c = d.run_program(program, 7) self.assertEqual(c.out[0], 1) program = d.read_program('day05/test4.txt') c = d.run_program(program, 8) self.assertEqual(c.out[0], 0) program = d.read_program('day05/test5.txt') c = d.run_program(program, 8) self.assertEqual(c.out[0], 1) program = d.read_program('day05/test5.txt') c = d.run_program(program, -8) self.assertEqual(c.out[0], 0) program = d.read_program('day05/test6.txt') c = d.run_program(program, 7) self.assertEqual(c.out[0], 1) program = d.read_program('day05/test6.txt') c = d.run_program(program, 8) self.assertEqual(c.out[0], 0)
def test_larger_example(self): program = d.read_program('day05/test9.txt') c = d.run_program(program, 6) self.assertEqual(c.out[0], 999) program = d.read_program('day05/test9.txt') c = d.run_program(program, 7) self.assertEqual(c.out[0], 999) program = d.read_program('day05/test9.txt') c = d.run_program(program, 8) self.assertEqual(c.out[0], 1000) program = d.read_program('day05/test9.txt') c = d.run_program(program, 9) self.assertEqual(c.out[0], 1001) program = d.read_program('day05/test9.txt') c = d.run_program(program, 10) self.assertEqual(c.out[0], 1001)
def __init__(self, path, addr): '''Creates a new Network Interface Controller using the Intcode program at the specified path and the specified address.''' self._comp = Computer(read_program(path)) self._addr = addr self._comp.in_.append(addr) self.run()
def part2(path): program = read_program(path) program[0] = 2 c = Computer(program) score = None paddle = None ball = None while c.ram[c.ip] != 99: # when prompted for controller input, have the paddle move toward the # ball, if their x coordinates are different if c.ram[c.ip] % 100 == 3: if paddle is None or ball is None: c.in_.append(0) elif paddle < ball: c.in_.append(1) elif paddle > ball: c.in_.append(-1) else: c.in_.append(0) c.step() if len(c.out) == 3: x, y, tile_id = (c.out.popleft() for _ in range(3)) if (x, y) == (-1, 0): score = tile_id elif tile_id == 3: paddle = x elif tile_id == 4: ball = x return score
def part1(path): c = Computer(read_program(path)) tiles = {id: set() for id in range(5)} while c.ram[c.ip] != 99: c.step() if len(c.out) == 3: x, y, tile_id = (c.out.popleft() for _ in range(3)) tiles[tile_id].add((x, y)) return len(tiles[2])
def shell(path): '''Runs an interactive springdroid shell, using the Intcode program stored at the specified path.''' cmp = Computer(read_program(path)) while cmp.ram[cmp.ip] != 99: out = run(cmp) print(out, end='' if type(out) == str else '\n') while True: instr = input() add_statement(cmp, instr) if instr in ('WALK', 'RUN'): break print('exiting shell')
def check_origin(path): '''Check if the origin contains the oxygen system. True if it does, False if it does not.''' robot = Computer(read_program(path)) # try each direction; if wall, try other direction if next_output(robot, 1) != 0: return next_output(robot, 2) == 2 elif next_output(robot, 2) != 0: return next_output(robot, 1) == 2 elif next_output(robot, 3) != 0: return next_output(robot, 4) == 2 elif next_output(robot, 4) != 0: return next_output(robot, 3) == 2 else: raise ValueError('surrounded by walls')
def part2(path, debug=False): ram = read_program(path) x, y = 0, 0 while True: if debug: print('part2', x, y) res_x = find_first_in_row(ram, y, x) if res_x is None: y += 1 elif check_top_right(ram, x, y): if check_bottom_row(ram, x, y): return (x, y) else: x += 1 else: y += 1
def run_program(path, start_white=False): '''Returns the Robot that runs the program loaded from path.''' program = read_program(path) c = Computer(program) r = Robot() paint_next = True if start_white: r.painted[(0, 0)] = 1 while c.ram[c.ip] != 99: if c.ram[c.ip] == 3: #waiting for input c.in_.append(r.get_color()) c.step() if c.out: out = c.out.popleft() if paint_next: r.paint(out) else: r.move(out) paint_next = not paint_next return r
def get_map(path): '''Get a full map of the oxygen system layout. Assumes origin is traversable and is NOT and oxygen location. Returns tuple: element 0 is a dictionary of (x,y) coordinates mapped to traversable neighbors; element 1 is a set of oxygen locations. ''' map_ = {} oxygen = set() orig = (0, 0) orig_robot = Computer(read_program(path)) to_search = deque([(orig, orig_robot)]) while to_search: loc, robot = to_search.popleft() neighbors = [] for nb in get_neighbors(loc): if nb[1] == loc[1] - 1: dir_ = 1 # north elif nb[1] == loc[1] + 1: dir_ = 2 # south elif nb[0] == loc[0] - 1: dir_ = 3 # west elif nb[0] == loc[0] + 1: dir_ = 4 # east else: raise ValueError(f'illegal neighbor {nb} to location {loc}') out = next_output(robot, dir_) if out != 0: neighbors.append(nb) if nb not in map_: to_search.append((nb, robot.clone())) if out == 2: oxygen.add(nb) # move back dir_ = {1: 2, 2: 1, 3: 4, 4: 3}[dir_] next_output(robot, dir_) map_[loc] = neighbors return (map_, oxygen)
def test_relative(self): '''Added for Day 9 extension of Intcode introducing relative mode. Testing programs that use relative mode extensions of existing opcodes. ''' program = d.read_program('day05/test10.txt') self.assertEqual(d.run_program(program).out, deque([9, 18, 1, 1]))
def print_part1(path): ram = read_program(path) for y in range(50): for x in range(50): print(get_drone_status(ram, x, y), end='') print()
def part1(path): ram = read_program(path) return sum( get_drone_status(ram, x, y) for x in range(50) for y in range(50))
def test_read_program(self): self.assertEqual(d.read_program('day05/test1.txt'), [1002, 4, 3, 4, 33])