def tests(): i = Intcode([1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50]) i.execute_next() assert i.memory == [1, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] i.execute_next() assert i.memory == [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] i = Intcode([1, 0, 0, 0, 99]) i.run() assert i.memory == [2, 0, 0, 0, 99] i = Intcode([2, 3, 0, 3, 99]) i.run() assert i.memory == [2, 3, 0, 6, 99] i = Intcode([2, 4, 4, 5, 99, 0]) i.run() assert i.memory == [2, 4, 4, 5, 99, 9801] i = Intcode([1, 1, 1, 4, 99, 5, 6, 0, 99]) i.run() assert i.memory == [30, 1, 1, 4, 2, 5, 6, 0, 99] assert i.pointer == 8 i.reset() assert i.memory == [1, 1, 1, 4, 99, 5, 6, 0, 99]
def part_two(ascii_program: Intcode): ascii_program.reset() ascii_program[0] = 2 main = "A,A,B,C,B,C,B,C,A,C\n" A = "R,6,L,8,R,8\n" B = "R,4,R,6,R,6,R,4,R,4\n" C = "L,8,R,6,L,10,L,10\n" feed = "n\n" input_ints = list(map(ord, main + A + B + C + feed)) return ascii_program.run(input_values=input_ints, halt_on_output=False)[-1]
def part2(data, target): cpu = Intcode() cpu.load_program(data) for i, j in product(range(100), range(100)): cpu.reset() cpu.ram[1] = i cpu.ram[2] = j cpu.run() if cpu.ram[0] == target: return 100 * i + j
def main(): vm = Intcode(PROGRAM, [1]) vm.compute() print("day 5/1", vm.outputs[:-1], vm.outputs[-1]) vm.reset(inputs=[5]) vm.compute() print("day 5/2", vm.outputs[:-1], vm.outputs[-1]) test()
def search(program, answer): vm = Intcode(program) for noun in range(100): for verb in range(100): if vm.reset(noun, verb).compute() == answer: return noun, verb raise Exception("brute force failed")
"""Day 9: Sensor Boost.""" from pathlib import Path from intcode import Intcode # Input input_file = Path(__file__).parent / 'BOOST_program.txt' boost = Intcode(str(input_file)) # Part 1: out = boost.run(1) print("BOOST test keycode:", out) assert out == 3839402290 # Part 2: boost.reset() distress_coordinates = boost.run(2) print("Coordinates of the distress signal:", distress_coordinates) assert distress_coordinates == 35734
from intcode import Intcode import thrusters # Input input_file = Path(__file__).parent / 'amplifier_controller_software.txt' amp = Intcode(str(input_file)) # Part 1: max_signal = 0 for phases in permutations(range(5)): thruster_signal = thrusters.thrusters(amp, phases) if thruster_signal > max_signal: max_signal = thruster_signal best_phases = phases print(f"Maximum output is {max_signal} " f"for phase sequence {best_phases}") assert max_signal == 117312 # Part 2: amplifiers = [Intcode(str(input_file), name=i) for i in 'ABCDE'] max_signal = 0 for phases in permutations(range(5, 10)): for amp in amplifiers: amp.reset() thruster_signal = thrusters.feedback_thrusters(amplifiers, phases) if thruster_signal > max_signal: max_signal = thruster_signal best_phases = phases print(f"Maximum output is {max_signal} " f"for phase sequence {best_phases} (with feedback loop)") assert max_signal == 1336480
#!/usr/bin/python3 from intcode import Intcode import itertools if __name__ == "__main__": # Build the machine with open('inputs/day2.txt', 'r') as f: ic = Intcode([int(inst) for inst in f.read().split(',')]) # Reset the 1202 error and run the machine, # then log final machine state ic.reset((1, 12), (2, 2)) ic.run() print("Memory[0] = %i" % ic.memory[0], sep="\n") # Iteratively guess values, run the machine for i, j in itertools.product(range(len(ic.memory)), range(len(ic.memory))): ic.reset((1, i), (2, j)) ic.run() if ic.memory[0] == 19690720: print("Memory[0] = 19690720 at i: %i, j: %i, 100*i+j = %i" % (i, j, 100 * i + j)) break
def tests(): ########################### #### Day02 tests #### ########################### i = Intcode([1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50]) i.execute_next() assert i[:] == [1, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] i.execute_next() assert i[:] == [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] i = Intcode([1, 0, 0, 0, 99]) i.run() assert i[:] == [2, 0, 0, 0, 99] i = Intcode([2, 3, 0, 3, 99]) i.run() assert i[:] == [2, 3, 0, 6, 99] i = Intcode([2, 4, 4, 5, 99, 0]) i.run() assert i[:] == [2, 4, 4, 5, 99, 9801] i = Intcode([1, 1, 1, 4, 99, 5, 6, 0, 99]) i.run() assert i[:] == [30, 1, 1, 4, 2, 5, 6, 0, 99] assert i.pointer == 8 i.reset() assert i[:] == [1, 1, 1, 4, 99, 5, 6, 0, 99] ########################### #### Day05 tests #### ########################### i = Intcode([1002, 4, 3, 4, 33]) i.run() assert i[:] == [1002, 4, 3, 4, 99] i = Intcode([1101, 100, -1, 4, 0]) i.run() assert i[:] == [1101, 100, -1, 4, 99] i = Intcode([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]) assert i.run(8) == 1 i.reset() assert i.run(66) == 0 i.reset() assert i.run(-8) == 0 i = Intcode([3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]) assert i.run(7) == 1 i.reset() assert i.run(8) == 0 i.reset() assert i.run(42) == 0 i = Intcode([3, 3, 1108, -1, 8, 3, 4, 3, 99]) assert i.run(8) == 1 i.reset() assert i.run(66) == 0 i.reset() assert i.run(-8) == 0 i = Intcode([3, 3, 1107, -1, 8, 3, 4, 3, 99]) assert i.run(7) == 1 i.reset() assert i.run(8) == 0 i.reset() assert i.run(42) == 0 i = Intcode([3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9]) assert i.run(0) == 0 i.reset() assert i.run(500) == 1 i.reset() assert i.run(-1) == 1 i = Intcode([3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1]) assert i.run(0) == 0 i.reset() assert i.run(500) == 1 i.reset() assert i.run(-1) == 1 i = Intcode([ 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99 ]) assert i.run(-123) == 999 i.reset() assert i.run(8) == 1000 i.reset() assert i.run(666) == 1001 ########################### #### Day09 tests #### ########################### i = Intcode([109, 19, 204, -34]) i.relative_base = 2000 i.execute_next() assert i.relative_base == 2019 i[1985] = 666 assert i.execute_next() == 666 quine = [ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ] i = Intcode(quine) assert i.run() == quine i = Intcode([1102, 34915192, 34915192, 7, 4, 7, 99, 0]) assert len(str(i.run())) == 16 i = Intcode([104, 1125899906842624, 99]) assert i.run() == 1125899906842624
def tests(): ########################### #### Day02 tests #### ########################### i = Intcode([1,9,10,3,2,3,11,0,99,30,40,50]) i.execute_next() assert i.memory == [1,9,10,70,2,3,11,0,99,30,40,50] i.execute_next() assert i.memory == [3500,9,10,70,2,3,11,0,99,30,40,50] i = Intcode([1,0,0,0,99]) i.run() assert i.memory == [2,0,0,0,99] i = Intcode([2,3,0,3,99]) i.run() assert i.memory == [2,3,0,6,99] i = Intcode([2,4,4,5,99,0]) i.run() assert i.memory == [2,4,4,5,99,9801] i = Intcode([1,1,1,4,99,5,6,0,99]) i.run() assert i.memory == [30,1,1,4,2,5,6,0,99] assert i.pointer == 8 i.reset() assert i.memory == [1,1,1,4,99,5,6,0,99] ########################### #### Day05 tests #### ########################### i = Intcode([1002,4,3,4,33]) i.run() assert i.memory == [1002,4,3,4,99] i = Intcode([1101,100,-1,4,0]) i.run() assert i.memory == [1101,100,-1,4,99] i = Intcode([3,9,8,9,10,9,4,9,99,-1,8]) assert i.run(8) == 1 i.reset() assert i.run(66) == 0 i.reset() assert i.run(-8) == 0 i = Intcode([3,9,7,9,10,9,4,9,99,-1,8]) assert i.run(7) == 1 i.reset() assert i.run(8) == 0 i.reset() assert i.run(42) == 0 i = Intcode([3,3,1108,-1,8,3,4,3,99]) assert i.run(8) == 1 i.reset() assert i.run(66) == 0 i.reset() assert i.run(-8) == 0 i = Intcode([3,3,1107,-1,8,3,4,3,99]) assert i.run(7) == 1 i.reset() assert i.run(8) == 0 i.reset() assert i.run(42) == 0 i = Intcode([3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9]) assert i.run(0) == 0 i.reset() assert i.run(500) == 1 i.reset() assert i.run(-1) == 1 i = Intcode([3,3,1105,-1,9,1101,0,0,12,4,12,99,1]) assert i.run(0) == 0 i.reset() assert i.run(500) == 1 i.reset() assert i.run(-1) == 1 i = Intcode([3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31, 1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104, 999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99]) assert i.run(-123) == 999 i.reset() assert i.run(8) == 1000 i.reset() assert i.run(666) == 1001
#BlueyNeilo 2019 #AoC Q.7a #Refactored from intcode import Intcode, parse_file from itertools import permutations AMPS = 5 ic = Intcode(parse_file("input.txt")) perms = list(permutations(range(AMPS))) #Permutations of [0..AMPS-1] max_out = 0 #Maximum output signal for p in perms: last_out = 0 for i in range(AMPS): ic.reset() ic.sim_input([p[i], last_out]) last_out = ic.execute_yield().pop() max_out = max(max_out, last_out) print(max_out)
current_loc, facing = move(current_loc, facing, output) computer.output = [] if not current_loc in panels: panels[current_loc] = 0 i += 1 return panels, current_loc, facing, i def painted_image(panels): x_min = min([k for k in panels], key=lambda x: x[0])[0] y_min = min([k for k in panels], key=lambda x: x[1])[1] x_max = max([k for k in panels], key=lambda x: x[0])[0] y_max = max([k for k in panels], key=lambda x: x[1])[1] x = x_max - x_min y = y_max - y_min picture = [['.' for _ in range(x + 1)] for _ in range(y + 1)] for panel, colour in panels.items(): picture[y_max - panel[1]][panel[0] - x_min] = '\u2588' if colour else '.' return '\n'.join(' '.join(p) for p in picture) program = utils.get_input(11) program = [int(i) for i in program[0].split(',')] computer = Intcode(program) first_star = len(operate_robot(computer, start_colour=0)[0]) computer.reset() second_star = '\n' + painted_image(operate_robot(computer, start_colour=1)[0]) utils.print_result(first_star, second_star)
class Robot: def __init__(self, program): self.core = Intcode(program, mode='ascii robot') self.grid = {} self.init_grid() self.seen = set() def init_grid(self): position = 0 + 0j for val in self._get_grid(): self.grid[position] = val if val == '\n': position = position - position.real - 1j else: position += 1 self.grid[position] = '\n' def _get_grid(self): while True: val = self.step() if val == -1: return 'END' yield chr(val) def step(self): self.core.waiting = False val = self.core.run() return val def _to_ascii(self, string, joiner=''): vals = [] for char in string: vals.append(ord(char)) return vals def check_output(self): while True: self.core.waiting = False val = self.core.run() self.seen.add(val) if val < 0 or val > 128: print(val) return else: print(chr(val), end='') if chr(val) in (':', '?'): self.core.waiting = False print(chr(self.core.run())) return def run(self, program, A, B, C, show=False): self.core.reset() self.core.program[0] = 2 if show: final = 'y\n' else: final = 'n\n' # step 1 -- draw map, ask for main self.check_output() # calculate all the inputs it wants inputs = [] for entry in (program, A, B, C, final): inputs.extend(self._to_ascii(entry)) # pass set of inputs for it to grab as needed self.core.secondary = inputs # run for function input val = self.core.run() # ask for function A: self.check_output() val = self.core.run() # ask for function B: self.check_output() val = self.core.run() # ask for function C: self.check_output() val = self.core.run() # ask for video feed: self.check_output() if show: self._run_video() else: self.check_output()
"""Day 5: Sunny with a Chance of Asteroids.""" from pathlib import Path from intcode import Intcode # Input input_file = Path(__file__).parent / 'TEST_diagnostic_program.txt' diagnostic = Intcode(str(input_file)) # Part 1: outs = diagnostic.run(1) print("Diagnostic for air conditioner unit:", outs) assert outs[-1] == 2845163 # Part 2: diagnostic.reset() out = diagnostic.run(5) print("Diagnostic for thermal radiator controller:", out) assert out == 9436229
def test(): program = [3,0,4,0,99] vm = Intcode(program, [123]) vm.compute() assert vm.outputs == [123] vm = Intcode(program, ["test"]) vm.compute() assert vm.outputs == ["test"] vm = Intcode([1002,4,3,0,99]) assert vm.compute() == 99 * 3 vm = Intcode(PROGRAM, [1]) vm.compute() assert all(x == 0 for x in vm.outputs[:-1]) assert vm.outputs[-1] == 14522484 vm = Intcode([3,9,8,9,10,9,4,9,99,-1,8]) for x in range(10): vm.reset(inputs=[x]) vm.compute() exp = 1 if x == 8 else 0 assert [exp] == vm.outputs vm = Intcode([3,9,7,9,10,9,4,9,99,-1,8]) for x in range(10): vm.reset(inputs=[x]) vm.compute() exp = 1 if x < 8 else 0 assert [exp] == vm.outputs vm = Intcode([3,3,1108,-1,8,3,4,3,99]) for x in range(10): vm.reset(inputs=[x]) vm.compute() exp = 1 if x == 8 else 0 assert [exp] == vm.outputs vm = Intcode([3,3,1107,-1,8,3,4,3,99]) for x in range(10): vm.reset(inputs=[x]) vm.compute() exp = 1 if x < 8 else 0 assert [exp] == vm.outputs vm = Intcode([3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9]) for x in [-1, 0, 1]: vm.reset(inputs=[x]) vm.compute() exp = 0 if x == 0 else 1 assert [exp] == vm.outputs vm = Intcode([3,3,1105,-1,9,1101,0,0,12,4,12,99,1]) for x in [-1, 0, 1]: vm.reset(inputs=[x]) vm.compute() exp = 0 if x == 0 else 1 assert [exp] == vm.outputs program = [3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31, 1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104, 999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99] vm = Intcode(program) for x, exp in [(7, 999), (8, 1000), (9, 1001)]: vm.reset(inputs=[x]) vm.compute() assert [exp] == vm.outputs vm = Intcode(PROGRAM, [5]) vm.compute() assert vm.outputs == [4655956]