def run(): network = { i: Computer(parse_file('input.intcode'), [i]) for i in range(50) } nat = None first_y = None while True: empty_outputs = all(c.output is None for c in network.values()) if empty_outputs and nat: network[0].inputs += nat for _, c in network.items(): c.output = None addr = c.iterate_once() x = c.iterate_once() y = c.iterate_once() if addr == 255: if first_y is None: first_y = y if nat and nat[1] == y: return first_y, y nat = [x, y] elif addr is None: c.inputs.append(-1) else: network[addr].inputs += [x, y]
def run(inputs): program = parse_file('game.intcode') c = Computer(program, inputs) o = '' while True: done = c.iterate() if done: break else: print(chr(c.output), end='') #o += chr(c.output) return o
def run_sprintscript(spring): program = parse_file('game.intcode') c = Computer(program) i = inputter([ord(c) for c in spring]) c.next_input = i.next o = c.run_to_output() for ch in o: if ch < 128: print(chr(ch), end='') else: print(ch) print() print()
#BlueyNeilo 2019 #AoC Q.5a #Refactored from intcode import Intcode, parse_file ic = Intcode(parse_file("input.txt")) ic.sim_input([1]) #Comment out for manual input ic.execute()
#!/usr/bin/env python3 if __name__ == '__main__': from intcode import IntcodeComputer, parse_file program = parse_file('input.txt') computer = IntcodeComputer(program) # Part 1 computer.run(argx=1) # Part 2 computer.reset() computer.run(argx=2)
from intcode import Computer, parse_file computer = Computer(parse_file("input.intcode")) outputs = computer.run_to_output() x, y = 0, 0 grid = {} robot = None for o in outputs: c = chr(o) if o == 10: y += 1 x = 0 else: grid[(x, y)] = c if c in ['v', '<', '>', '^']: robot = (x, y) x += 1 def neighbours(point): x, y = point return [ ('R', (x + 1, y)), ('L', (x - 1, y)), ('D', (x, y + 1)), ('U', (x, y - 1)), ] def is_intersection(point):
def check(x, y): inputs = inputter([x, y]) computer = Computer(parse_file('input.intcode')) computer.next_input = inputs.next computer.iterate() return computer.output == 1
self.op1() elif self.current_op == '02': self.op2() elif self.current_op == '03': self.read_paint() self.op3() elif self.current_op == '04': self.op4() if self.should_paint() is True: self.paint() elif self.current_op == '05': self.op5() elif self.current_op == '06': self.op6() elif self.current_op == '07': self.op7() elif self.current_op == '08': self.op8() elif self.current_op == '09': self.op9() except Exception: print('error {} at P{}'.format('run', self.position)) if __name__ == '__main__': file = parse_file('input.txt') robby = Robot(file) robby.run()
new_horizon = [] for p in horizon: for _, point in neighbours(p): if grid.get(point, 1) == 0: continue if end is not None and point == end: return distance + 1 if point not in visited: new_horizon.append(point) visited.add(point) horizon = new_horizon distance += 1 return distance grid = build_grid(parse_file("input.intcode")) for k, v in grid.items(): if v == 2: oxygen = k break one = bfs(grid, (0, 0), oxygen) two = bfs(grid, oxygen) - 1 print('Part 1:', one) print('Part 2:', two)
#BlueyNeilo 2019 #AoC Q.7b #Refactored from intcode import Intcode, parse_file from itertools import permutations AMPS = 5 program = parse_file("input.txt") ics = [Intcode(program[:]) for _ in range(AMPS)] perms = list(permutations(range(AMPS, 2*AMPS))) max_out = 0 for p in perms: last_out = 0 last_iter = -1 for ic in ics: ic.reset() while last_out > last_iter: last_iter = last_out for i in range(AMPS): if last_iter == 0: ics[i].sim_input([p[i]]) ics[i].sim_input([last_out]) unwrap = ics[i].execute_yield() if unwrap:
x = computer.output computer.iterate() y = computer.output done = computer.iterate() tile = computer.output if tile == 4: ball = x if tile == 3: pad = x if x == -1 and y == 0: score = tile if done: return score program = parse_file('game.intcode') outputs = run_to_output(program) blocks = 0 for i, v in enumerate(outputs): if (i + 1) % 3 == 0: if v == 2: blocks += 1 print("Part 1:", blocks) program[0] = 2 print("Part 2:", run_with_screen(program))