class RescueDrone: def __init__(self, code): self.brain = Intcode() self.brain.load(code) self.commands = { "n": "north", "s": "south", "e": "east", "w": "west", "t": "take", "d": "drop", "i": "inv" } def act(self, command=""): if command != "": if command[0] in self.commands: command = self.commands[command[0]] + command[1:] for c in command: self.brain.add_input(ord(c)) self.brain.add_input(ord("\n")) self.brain.run() outputs = map(chr, map(int, self.brain.get_all_outputs().split())) self.print_output(outputs) def print_output(self, outputs): for c in outputs: print(c, end="")
class Arcade: def __init__(self): self.cpu = Intcode() self.screen = {} def load_game(self, code): self.cpu.load(code) def insert_coins(self, n): self.cpu.code[0] = 2 def draw_screen(self): tiles = {0: "░░", 1: "██", 2: "▓▓", 3: "==", 4: "()"} if (-1, 0) in self.screen: score = self.screen[(-1, 0)] else: score = -1 print(f"██ {score:60d} points ██") for row in range(20): for col in range(38): if (col, row) in self.screen: tile = tiles[self.screen[(col, row)]] print(tile, end="") print() def auto_move(self): for x, y in self.screen.keys(): if self.screen[(x, y)] == 3: paddle = x if self.screen[(x, y)] == 4: ball = x if paddle < ball: return "d" elif paddle > ball: return "a" else: return "s" def run(self, automatic): inputs = {"": 0, "a": -1, "s": 0, "d": 1, "j": -1, "k": 0, "l": 1} while self.cpu.state != "halted": self.cpu.run() while len(self.cpu.outputs): if len(self.cpu.outputs) >= 3: x = self.cpu.outputs.popleft() y = self.cpu.outputs.popleft() tile = self.cpu.outputs.popleft() self.screen[(x, y)] = tile else: print( f"ERROR: Encountered a faulty cpu output: {list(self.cpu.outputs)}" ) break if self.cpu.state != "halted": self.draw_screen() if not automatic: inp = input("move: ") else: inp = self.auto_move() self.cpu.add_input(inputs[inp])
def ask_about_position(data, x, y): beam = Intcode() beam.load(data) beam.add_input(x) beam.add_input(y) beam.run() return beam.get_output()
class EmHuPaR: def __init__(self): self.x = 0 self.y = 0 self.dir = 0 # up self.brain = None def load(self, program): self.brain = Intcode() self.brain.load(program) self.brain.exit_on_output = True def run(self, panels): dx = {0: 0, 1: 1, 2: 0, 3: -1} dy = {0: -1, 1: 0, 2: 1, 3: 0} while True: self.brain.set_input(panels[(self.x, self.y)]) self.brain.run() if self.brain.state == "halted": return color = self.brain.get_output() self.brain.run() turn = self.brain.get_output() panels[(self.x, self.y)] = color if turn == 1: self.dir = (self.dir + 1) % 4 else: self.dir = (self.dir + 3) % 4 self.x += dx[self.dir] self.y += dy[self.dir]
class VacuumRobot: def __init__(self): self.brain = Intcode() def load(self, code): self.brain.load(code) def grab_camera(self): self.brain.run() return "".join(map(chr, map(int, self.brain.get_all_outputs().split()))) def run_path(self, order, subpaths): self.brain.code[0] = 2 # switch to the path following mode line = list(map(ord, ",".join(map(str, order)) + "\n")) for c in line: self.brain.add_input(c) for path in subpaths: line = list(map(ord, ",".join(map(str, path)) + "\n")) for c in line: self.brain.add_input(c) line = list(map(ord, "n\n")) for c in line: self.brain.add_input(c) self.brain.run() outputs = self.brain.get_all_outputs().split() return outputs[-1]
def run(): data = load_data("Day09.txt") comp = Intcode() comp.load(data) comp.add_input(1) comp.run() print(f"The BOOST keycode is {comp.get_output()}") comp.load(data) comp.add_input(2) comp.run() print(f"The coordinate is {comp.get_output()}")
def __init__(self, count, code): self.nodes = [] self.queues = [] self.iddles = [0] * count self.nat = (0, 0) self.nat_timer = 0 self.nat_history = set() for i in range(count): node = Intcode() node.load(code) node.add_input(i) self.nodes.append(node) self.queues.append(deque())
class SpringDroid: def __init__(self, data): self.brain = Intcode() self.brain.load(data) def walk(self, code): for line in code: for char in line: self.brain.add_input(ord(char)) self.brain.add_input(ord("\n")) for c in "WALK": self.brain.add_input(ord(c)) self.brain.add_input(ord("\n")) self.brain.run() output = list(map(int, self.brain.get_all_outputs().split())) return output def run(self, code): for items in code: line = " ".join(items) for char in line: self.brain.add_input(ord(char)) self.brain.add_input(ord("\n")) for c in "RUN": self.brain.add_input(ord(c)) self.brain.add_input(ord("\n")) self.brain.run() output = list(map(int, self.brain.get_all_outputs().split())) return output def run_and_score(self, code): for items in code: line = " ".join(items) for char in line: self.brain.add_input(ord(char)) self.brain.add_input(ord("\n")) for c in "RUN": self.brain.add_input(ord(c)) self.brain.add_input(ord("\n")) self.brain.run() output = list(map(int, self.brain.get_all_outputs().split())) outcome = output[-1] if output[-1] > 255 else 0 return self.brain.instructions_executed, outcome
def find_tank(d, pos, data, trace): global order, loc # Is the requested direction already explored? old_pos = pos pos = tuple(map(operator.add, pos, p[d])) if pos in explored: return # Load a copy of the bot comp = Intcode() bot = comp.load(data) # Send it in the requested direction r = bot.send(d) if r == 0: explored[pos] = "." pos = old_pos elif r == 1: explored[pos] = " " trace.append(pos) elif r == 2: explored[pos] = "X" order = trace loc = old_pos trace.append(pos) for x in p.keys(): find_tank(x, pos, comp.save(), trace.copy())
class RepairDroid: def __init__(self): self.brain = Intcode() self.x = 0 self.y = 0 def load(self, code): self.brain.load(code) def show_map(self, corridors): x0, x1, y0, y1 = 0, 0, 0, 0 for place in corridors.keys(): if place[0] < x0: x0 = place[0] if place[0] > x1: x1 = place[0] if place[1] < y0: y0 = place[1] if place[1] > y1: y1 = place[1] for y in range(y0, y1 + 1): for x in range(x0, x1 + 1): if x == self.x and y == self.y: c = "[]" elif (x, y) in corridors: if corridors[(x, y)] == -1: c = "░░" elif corridors[(x, y)] == -2: c = "██" elif corridors[(x, y)] == -10: c = "GG" else: c = " " print(c, end="") print() def explore(self, corridors): direction = 0 dir_to_movement = {0: 1, 1: 4, 2: 2, 3: 3} dx = {0: 0, 1: 1, 2: 0, 3: -1} dy = {0: -1, 1: 0, 2: 1, 3: 0} while True: if self.x == 0 and self.y == 0 and len( corridors) > 10: # back at the beginning break self.brain.add_input(dir_to_movement[direction]) self.brain.run() info = self.brain.get_output() if info == 0: # hit a wall corridors[(self.x + dx[direction], self.y + dy[direction])] = -2 direction = (direction + 1) % 4 elif info == 1: # moved self.x += dx[direction] self.y += dy[direction] corridors[(self.x, self.y)] = -1 direction = (direction + 3) % 4 elif info == 2: # found generator self.x += dx[direction] self.y += dy[direction] corridors[(self.x, self.y)] = -10 direction = (direction + 3) % 4 self.show_map(corridors)
paddlex = x return points, ballx, paddlex print("#--- part1 ---#") program = list(map(int, read_file('13.txt')[0].split(','))) vm = Intcode() vm.run(Task('part1', program, [], ramsize=4096)) print(vm.tasks[0].outputs[2::3].count(2)) print("#--- part2 ---#") program = list(map(int, read_file('13.txt')[0].split(','))) vm = Intcode() vm.load(Task('part2', program, [], ramsize=4096)) vm.tasks[0].mem[0] = 2 points, ballx, paddlex = 0, 0, 0 state = vm.run() while state != Intcode.STATE_DONE: points, ballx, paddlex = parse_outputs(vm.tasks[0].outputs, points, ballx, paddlex) joy = 0 if ballx > paddlex: joy = 1 elif ballx < paddlex: joy = -1 vm.send(vm.tasks[0], joy) state = vm.run()
if (x, y) in grid: color = grid[(x, y)] else: color = COLOR_BLACK vm.send(task, color) state = vm.run() return grid print("#--- part1 ---#") program = list(map(int, read_file('11.txt')[0].split(','))) vm = Intcode() task = Task('part1', program, [], ramsize=4096) vm.load(task) grid = dict() grid[(0, 0)] = COLOR_BLACK print(len(run(task, grid))) print("#--- part2 ---#") program = list(map(int, read_file('11.txt')[0].split(','))) vm = Intcode() task = Task('part2', program, [], ramsize=4096) vm.load(task) grid = dict() grid[(0, 0)] = COLOR_WHITE grid = run(task, grid)