def execute(springcode, verbose=False): """ run the springcode and return the large integer if the bot succeeds. if the bot fails, return the row that killed it. prints all output if verbose is True. """ computer = ic.Computer(ic.load("input")) for c in s: computer.send(ord(c)) rows = [[]] #computer.program[1665] = 0 #flight mode while True: if computer.pc == 610: print(f"Packed data: {computer.program[753]:3}; unpacked data:", [computer.program[x] for x in range(716, 747)]) # for i in range(726, 734): #no gap mode # computer.program[i]= 1 if (x := computer.tick()) is not None: if x > 256: return x else: if verbose: print(chr(x), end="") if chr(x) == "\n": rows[-1] = "".join(rows[-1]) if all(x in rows[-1] for x in "@#"): return rows[-1].replace("@", ".") rows.append([]) else: rows[-1].append(chr(x))
def paint(starting_color): computer = ic.Computer(ic.load("input")) field = defaultdict(int) pos = Point(0, 0) facing = up field[pos] = starting_color commands = [] seen = set() #probably don't really need this while not computer.halted: if computer.needs_input(): computer.send(field[pos]) response = computer.tick() if response is not None: commands.append(response) if len(commands) == 2: color, direction = commands field[pos] = color seen.add(pos) if direction == 0: facing = counterclockwise[facing] else: facing = clockwise[facing] pos += facing commands.clear() return field
def feedback_power(signals): computers = [ic.Computer(program, [signal]) for signal in signals] computers[0].send(0) while not computers[-1].halted: if all (c.halted or c.needs_input() for c in computers): raise Exception("Deadlock") for i, c in enumerate(computers): if not c.needs_input() and not c.halted: out = c.tick() if out is not None: computers[(i+1)%len(computers)].send(out) return computers[-1].outputs[-1]
def play(): if os.path.exists(SAVE_FILENAME) and ask("Load previous save? "): computer = load() else: computer = ic.Computer(ic.load("input")) while True: if computer.needs_input(): command = input() if command == "save": with open(SAVE_FILENAME, "w") as file: file.write(computer.dumps()) print("Saved. Command?") else: command = command + "\n" for c in command: computer.send(ord(c)) else: x = computer.tick() if x is not None: print(chr(x), end="") sys.stdout.flush()
def iter_output(): computer = ic.Computer(ic.load("input")) while not computer.halted: x = computer.tick() if x is not None: yield (chr(x))
intersections = [] for p, cell in list(field.items()): if cell != EMPTY: neighboring_scaffolds = 0 for delta in deltas: neighbor = p + delta if field[neighbor] != EMPTY: neighboring_scaffolds += 1 if neighboring_scaffolds > 2: intersections.append(p) print(sum(p.x * p.y for p in intersections)) #part 2 #value determined by looking at the output of longpath() very intently s = "B,A,B,C,A,C,A,C,B,C\nL,8,R,10,R,6\nR,12,L,10,R,12\nR,12,L,10,R,10,L,8\nn\n" computer = ic.Computer(ic.load("input")) computer.program[0] = 2 for c in s: computer.send(ord(c)) while not computer.halted: x = computer.tick() if x is not None: if x > 256: print(x) else: #print(chr(x), end="") pass
break if x == -1 and y == 0: print("Score:", value) booted = True break if value == 3: paddle_x = x if value == 4: ball_x = x color = ["white", "black", "red", "green", "blue"][value] canvas.itemconfig(cell_ids[x, y], fill=color) if booted: break root.after(10, update) program = ic.load("input") program[0] = 2 computer = ic.Computer(program) root = tkinter.Tk() cols = 46 rows = 24 cell_size = 10 canvas = tkinter.Canvas(root, width=cell_size * cols, height=cell_size * rows) canvas.pack() cell_ids = {} for i in range(cols): for j in range(rows): id = canvas.create_rectangle(i * cell_size, j * cell_size, (i + 1) * cell_size,
import intcomputer program = intcomputer.load("input") print(intcomputer.Computer(program, [1]).tick_until_output()) print(intcomputer.Computer(program, [2]).tick_until_output())
def get(x,y): computer = ic.Computer(ic.load("input")) computer.send(x) computer.send(y) return computer.tick_until_output()
import intcomputer as ic for x in (1, 5): print(ic.Computer(ic.load("input"), [x]).tick_until_blocked()[-1])
import intcomputer as ic computers = [ic.Computer(ic.load("input"), [i]) for i in range(50)] output_bufs = [[] for _ in range(50)] IDLE = 0 BUSY = 1 time = 0 idle_states = [BUSY for _ in range(50)] part_one_answered = False nat_x = None nat_y = None last_sent_y = None while True: time += 1 for idx, (computer, output_buf) in enumerate(zip(computers, output_bufs)): if computer.needs_input(): computer.send(-1) if computer.program[61] == 1: idle_states[idx] = IDLE x = computer.tick() if x is not None: output_buf.append(x) if len(output_buf) == 3: pc, x, y = output_buf output_buf.clear() if pc == 255: if not part_one_answered:
def amplify(signal, input): return ic.Computer(program, [input, signal]).tick_until_blocked()[-1]