class Arcade: def __init__(self, game_code): self.comp = Intcomp(game_code) self.comp.output_func = ArgumentCombiner(self.process_output) self.comp.input_func = self.process_input self.screen = {} self.score = 0 self.ball_x = 0 self.paddle_x = 0 def process_input(self): return cmp(self.ball_x - self.paddle_x, 0) def process_output(self, x, y, tile_id): if x == -1 and y == 0: self.score = tile_id else: self.screen[(x, y)] = tile_id if tile_id == 4: self.ball_x = x if tile_id == 3: self.paddle_x = x def play(self): self.comp.run()
def get_tiles(data: List[int]) -> Dict[complex, int]: comp = Intcomp(data) comp.run() log = comp.full_output() tiles = {} for i in range(len(log) // 3): tiles[complex(log[i * 3], log[i * 3 + 1])] = log[i * 3 + 2] return tiles
def __init__(self, game_code): self.comp = Intcomp(game_code) self.comp.output_func = ArgumentCombiner(self.process_output) self.comp.input_func = self.process_input self.screen = {} self.score = 0 self.ball_x = 0 self.paddle_x = 0
def registration(intcode: List[int], showplots: bool): comp = Intcomp(intcode) robot = PaintRobot(100, start_on_white=True) paint_hull(comp, robot) if showplots: plt.imshow(robot.hull) plt.show()
def panels_painted(intcode: List[int], showplots: bool) -> int: comp = Intcomp(intcode) robot = PaintRobot(200) paint_hull(comp, robot) if showplots: plt.imshow(robot.hull) plt.show() return robot.panelspainted
def findIntersections(data: List[int]): comp = Intcomp(data) comp.run() output = comp.full_output() drawScaffolding(output) x = 0 y = 0 scaffolding = set() intersections = set() robot = None for number in output: if number == 35: scaffolding.add((x, y)) if {(x - 1, y - 1), (x, y - 1), (x + 1, y - 1), (x, y - 2)}.issubset(scaffolding): intersections.add((x, y - 1)) elif number == 94: robot = (x, y) if number == 10: y += 1 x = 0 else: x += 1 return sum(x * y for x, y in intersections)
def find_oxygen(data: List[int]): def move_robot(goal): if distance(robot.position, attempt) > 1: path = find_path(robot.position, goal, grid) for pos in reversed(path): comp.add_input(direction(robot.position, pos)) out = comp.next_output() if out != 1: print('invalid path?') exit() robot.position = pos # final step comp.add_input(direction(robot.position, goal)) comp = Intcomp(data) grid = np.zeros([50, 50]) # 0 unknown, 1 traversable, 2 walls, 3 oxygen start = 25 + 25j robot = Robot(start) grid[robot.y, robot.x] = 1 # start position is traversable to_visit = neighbors(robot.position) len_to_oxygen = 0 to_oxygenate = [] # for part 2 while to_visit: attempt = to_visit.pop( ) # get latest coord added (to minimize travel distance?) move_robot(attempt) output = comp.next_output() if output == 2: # oxygen found robot.position = attempt grid[robot.y, robot.x] = 3 len_to_oxygen = len(find_path(start, robot.position, grid)) + 1 to_oxygenate.append(robot.position) elif output == 1: robot.position = attempt grid[robot.y, robot.x] = 1 for neighbor in neighbors(robot.position): if not grid[int(neighbor.imag), int(neighbor.real)]: # if unknown to_visit.append(neighbor) elif output == 0: grid[int(attempt.imag), int(attempt.real)] = 2 else: print('unknown output from intcomputer:', output) exit() minutes = -1 # -1 as the first minute is for the spread in location of oxygen system while to_oxygenate: spread = [] for pos in to_oxygenate: grid[int(pos.imag), int(pos.real)] = 3 for neighbor in neighbors(pos): if grid[int(neighbor.imag), int(neighbor.real)] == 1: spread.append(neighbor) minutes += 1 to_oxygenate = spread #plt.imshow(grid) #plt.show() return len_to_oxygen, minutes
from intcomp import Intcomp, parse_input if __name__ == "__main__": c = Intcomp(parse_input("input")) c.run()
def find_keycode(intcode: List[int]) -> int: intcomp = Intcomp(intcode, [1]) intcomp.run() return intcomp.next_output()
def get_coordinates(intcode: List[int]) -> int: intcomp = Intcomp(intcode, [2]) intcomp.run() return intcomp.next_output()
def run(settings): highest_signal = 0 for setting in settings: mem = IoMem({ "a": [setting[0], 0], # init "b": [setting[1]], "c": [setting[2]], "d": [setting[3]], "e": [setting[4]] }) a = Intcomp(amplifier_program, lambda: mem.input("a"), lambda o: mem.output("b", o)) b = Intcomp(amplifier_program, lambda: mem.input("b"), lambda o: mem.output("c", o)) c = Intcomp(amplifier_program, lambda: mem.input("c"), lambda o: mem.output("d", o)) d = Intcomp(amplifier_program, lambda: mem.input("d"), lambda o: mem.output("e", o)) e = Intcomp(amplifier_program, lambda: mem.input("e"), lambda o: mem.output("a", o)) while True: next(a.run()) next(b.run()) next(c.run()) next(d.run()) if next(e.run()) is ExitStatus.HALTED: break highest_signal = max(highest_signal, mem.input("a")) return highest_signal