def execute_springscript(springscript, intcode): computer = IntcodeComputer(intcode) res = computer.execute([ord(c) for c in springscript]) view_str = ''.join([chr(v) for v in res if v < 256]) print(view_str) print(res[-1])
def main(): with open("input23") as f: intcode = [int(v) for v in f.read().split(',')] # Init 50 computers computersbyaddress = {} for i in range(50): computer = IntcodeComputer(intcode) # Set computer address computer.inputs = [i] computersbyaddress[i] = computer # Run network IO for each computer one by one natx = None naty = None previousnatysent = None while True: idle = True for computer in computersbyaddress.values(): # add -1 to input if there's nothing in queue if not computer.inputs: computer.inputs.append(-1) else: idle = False output = computer.execute([]) while len(output) >= 3: idle = False # get packet sent by this computer address = output.pop(0) x = output.pop(0) y = output.pop(0) # Nat management if address == 255: print("packet sent to nat(255): {} {}".format(x, y)) natx = x naty = y else: # Put it in the target computer queue computersbyaddress[address].inputs.extend([x, y]) if idle: # Idle network, nat send store packet to addresse 0 computersbyaddress[0].inputs.extend([natx, naty]) if previousnatysent == naty: print( "sending same y value twice in a row: {}".format(naty)) return previousnatysent = naty
print(line) print(score) with open("input13") as f: intcode = [int(v) for v in f.read().split(',')] computer = IntcodeComputer(intcode) computer.write(0, 2) screen = {} score = 0 cmd = 0 while not computer.terminated: res = computer.execute([cmd]) for i in range(0, len(res), 3): x = res[i] y = res[i + 1] val = res[i + 2] if x == -1 and y == 0: score = val else: screen[(x, y)] = val if val == 4: ballxpos = x elif val == 3: paddlexpos = x
def isonbeam(x, y, intcode): computer = IntcodeComputer(intcode) res = computer.execute([x, y]) return res[0]
from intcodecomputer import IntcodeComputer with open("input9") as f: intcode = [int(v) for v in f.read().split(',')] computer = IntcodeComputer(intcode) print(computer.execute([2]))
with open("input11") as f: intcode = [int(v) for v in f.read().split(',')] computer = IntcodeComputer(intcode) pos = (0, 0) orientation = 0 # part 1 # whitepanels = set() # part 2 whitepanels = {(0, 0)} paintedpanels = set() while not computer.terminated: currentpanelcolor = 1 if pos in whitepanels else 0 color, turn = computer.execute([currentpanelcolor]) paintedpanels.add(pos) if color == 1: whitepanels.add(pos) else: whitepanels.discard(pos) if turn == 1: orientation = (orientation + 1) % 4 else: orientation = (orientation - 1) % 4 if orientation == 0: pos = (pos[0], pos[1] - 1)
def main(): with open("input17") as f: intcode = [int(v) for v in f.read().split(',')] computer = IntcodeComputer(intcode) res = computer.execute([]) view_str = ''.join([chr(v) for v in res]) lines = view_str.splitlines() for line in lines: print(line) position = None orientation = None # Part 1 total = 0 for y, line in enumerate(lines): for x, char in enumerate(line): try: if (char == '#' and lines[y - 1][x] == '#' and lines[y + 1][x] == '#' and lines[y][x - 1] == '#' and lines[y][x + 1] == '#'): total += x * y except IndexError: pass if char in orientations: position = (x, y) orientation = char print(total) # Part 2 # Find path commands = [] while True: tx, ty = calculatetargetcoords(position, orientation) try: target = lines[ty][tx] except IndexError: target = None pass if target == "#": commands.append('1') position = (tx, ty) else: for turn in ['L', 'R']: targetorienation = calculatetargetorientation( orientation, turn) tx, ty = calculatetargetcoords(position, targetorienation) try: target = lines[ty][tx] except IndexError: target = None pass if target == "#": # This direction looks promising commands.append(turn) orientation = targetorienation break else: # Could not find anywhere to get next, must be the end break commands = list(aggregatesones(commands)) # Attempt to divide the commands in 3 routines covering everything routines = findpatternscoverall(commands, [False] * len(commands), 3) routinecalls = [] i = 0 while i < len(commands): # Find routine to use for routineid, routine in enumerate(routines): if commands[i:i + len(routine)] == routine: routinecalls.append(['A', 'B', 'C'][routineid]) i += len(routine) break else: raise Exception("Could not find routine") # Add robot subroutines to call robotinputs = commandstostring(routinecalls) + "\n" # Add commands for each subroutines for routine in routines: robotinputs += commandstostring(routine) + "\n" # Add continous video feed 'no' robotinputs += "n\n" print(robotinputs) # Send this to the robot computer = IntcodeComputer(intcode) computer.write(0, 2) res = computer.execute([ord(c) for c in robotinputs]) view_str = ''.join([chr(v) for v in res[:-1]]) print(view_str) # Final ouput value print(res[-1])