예제 #1
0
 def testone(self):
     m = IntCodeMachine([1002, 4, 3, 4, 33])
     try:
         while True:
             next(m.run())
     except StopIteration:
         assert m.memory[0:5] == [1002, 4, 3, 4, 99]
예제 #2
0
 def testnegative(self):
     m = IntCodeMachine([1101, 100, -1, 4, 0])
     try:
         while True:
             next(m.run())
     except StopIteration:
         assert m.memory[0:5] == [1101, 100, -1, 4, 99]
예제 #3
0
def run():
    memory = open_file("input_day17.txt")
    memory[0] = 2
    machine = IntCodeMachine(memory, onread=oninput, onwrite=onoutput)
    machine.run()
    for line in scaffold:
        print(''.join(line))
예제 #4
0
def run():
    memory = open_file("input_day17.txt")
    machine = IntCodeMachine(memory, None, onwrite=onoutput)
    machine.run()

    for line in scaffold:
        print(''.join(line))

    print(intersections())
예제 #5
0
def run():
    memory = read_input()
    robot = Robot()
    robot.squares[robot.pos] = 1
    machine = IntCodeMachine(memory, robot.read, robot.instruct)
    machine.run()
    
    for y in range(6):
        print(''.join([' ', '█'][robot.squares[x + y * 1j]] for x in range(42)))
예제 #6
0
 def testinputmode(self):
     m = IntCodeMachine([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8], [1])
     assert next(m.run()) == 0, 'result is not False for val < 8'
     m = IntCodeMachine([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8], [8])
     assert next(m.run()) == 1, 'result is not True for val == 8'
     m = IntCodeMachine([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8], [10])
     assert next(m.run()) == 0, 'result is not False for val > 8'
예제 #7
0
def run():
    memory = read_input()
    machine = IntCodeMachine(memory, None, output.append)
    machine.run()
    coords = defaultdict(int)
    for n in range(0, len(output), 3):
        x, y, t = output[n:n + 3]
        coords[(x, y)] = t

    print(coords.values())
    print(len([t for t in coords.values() if t == 2]))
예제 #8
0
def run():
    memory = read_input()
    robot = Robot()
    machine = IntCodeMachine(memory, robot.oninput, robot.onoutput)
    try:
        machine.run()
    except RuntimeError:
        draw_map(robot.map, (0, 0))
        print("Part 1", robot.solution, robot.oxygen)

    oxygen(robot.map, robot.oxygen)
예제 #9
0
 def testrelativebase(self):
     m = IntCodeMachine([
         109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101,
         0, 99
     ])
     try:
         while True:
             next(m.run())
     except StopIteration:
         assert m.output == [
             109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006,
             101, 0, 99
         ]
예제 #10
0
 def testlesseightimmode(self):
     m = IntCodeMachine([3, 3, 1107, -1, 8, 3, 4, 3, 99])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 1, 'result is not True for val < 8'
     m.reset()
     with mock.patch('builtins.input', return_value=8):
         assert next(m.run()) == 0, 'result is not False for val == 8'
     m.reset()
     with mock.patch('builtins.input', return_value=10):
         assert next(m.run()) == 0, 'result is not False for val > 8'
예제 #11
0
 def testequalseightposmode(self):
     m = IntCodeMachine([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 0, 'result is not False for val < 8'
     m.reset()
     with mock.patch('builtins.input', return_value=8):
         assert next(m.run()) == 1, 'result is not True for val == 8'
     m.reset()
     with mock.patch('builtins.input', return_value=10):
         assert next(m.run()) == 0, 'result is not False for val > 8'
예제 #12
0
def run():
    memory = open_file("input_day19.txt")
    for y in range(max_y):
        for x in range(max_x):
            lst = [y, x]
            print(lst)
            machine = IntCodeMachine(memory.copy(), lst.pop,
                                     lambda v: update_grid(x, y, v))
            machine.run()

    for line in grid:
        print(''.join(str(x) for x in line))

    print(sum(sum(line) for line in grid))
예제 #13
0
def run():
    instructions = open_file("input_day07.txt")

    best = 0
    for perm in itertools.permutations([5, 6, 7, 8, 9]):
        machines = []
        machine_io = []
        for n in range(5):
            io = MachineIO(machine_io, n, perm[n])
            machine_io.append(io)
            machines.append(
                IntCodeMachine(instructions.copy(), io.oninput, io.onoutput))

        # Kick off signal, since the last machine feeds into the first, we put it there
        machine_io[-1].output.append(0)

        while not (all(machine.halted for machine in machines)):
            for machine, io in zip(machines, machine_io):
                while not machine.halted:
                    machine.step()
                    if io.output:
                        break

        best = max(machine_io[-1].max_output, best)
    print(best)
예제 #14
0
def loop(memory):
    for u_y in range(1059 - 2, 1059 + 3):
        l_y = u_y + 99

        x = int(l_y * 0.7586)

        for l_x in range(x - 1, x + 2):
            r_x = l_x + 99

            outputs = []
            for x,y in [(l_x, u_y), (r_x, u_y), (l_x, l_y), (r_x, l_y)]:
                lst = [y, x]
                machine = IntCodeMachine(memory.copy(), lst.pop, outputs.append)
                machine.run()
                if (outputs == [1,1,1,1]):
                    return(l_x, u_y)
예제 #15
0
 def test(self):
     m = IntCodeMachine([
         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
     ])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 999, 'result is not 999 for val < 8'
     m.reset()
     with mock.patch('builtins.input', return_value=8):
         assert next(m.run()) == 1000, 'result is not 1000 for val == 8'
     m.reset()
     with mock.patch('builtins.input', return_value=10):
         assert next(m.run()) == 1001, 'result is not 1001 for val > 8'
예제 #16
0
def initialize_computers(memory, number, nat):
    computers = []
    input_queues = {}
    for n in range(number):
        in_queue = NetworkInput(n)
        out_queue = NetworkOutput(input_queues, n)
        input_queues[n] = in_queue
        computers.append(
            IntCodeMachine(memory.copy(), in_queue.get, out_queue.put))

    input_queues[255] = nat
    return computers, input_queues
예제 #17
0
def run():
    memory = read_input()

    machine = IntCodeMachine(memory, None, output.append)
    machine.run()

    field = load_field(glyphs)
    memory = read_input()
    memory[0] = 2

    draw_field(field)
    game = Game(field)
    machine2 = IntCodeMachine(memory, game.oninput, game.onoutput)
    machine2.run()
예제 #18
0
 def testjumponeimmode(self):
     m = IntCodeMachine([3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 1, 'result is not True for val != 0'
     m.reset()
     with mock.patch('builtins.input', return_value=0):
         assert next(m.run()) == 0, 'result is not False for val == 0'
예제 #19
0
 def testjumponeposmode(self):
     m = IntCodeMachine(
         [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 1, 'result is not True for val != 0'
     m.reset()
     with mock.patch('builtins.input', return_value=0):
         assert next(m.run()) == 0, 'result is not False for val == 0'
예제 #20
0
def run():
    best, best_perm = 0, None
    for perm in itertools.permutations([0, 1, 2, 3, 4]):
        result = [0]
        for phase in perm:
            input_list = [result.pop(), phase]
            instructions = open_file("input_day07.txt")
            IntCodeMachine(instructions, input_list.pop, result.append).run()

        if result[-1] > best:
            best, best_perm = result[-1], perm
            best_perm = perm

    print(best, best_perm)
예제 #21
0
def boot_amplifiers(program, phase):
    amps = [IntCodeMachine(program) for i in range(0, len(phase))]
    energy = 0
    for i in range(0, len(phase)):
        amps[i].set_inputs([phase[i], energy])
        energy = next(amps[i].run())

    while all(not amp.isStopped for amp in amps):
        for i in range(0, len(phase)):
            try:
                amps[i].set_inputs([energy])
                energy = next(amps[i].run())
            except StopIteration:
                pass

    return energy
예제 #22
0
def run():
    memory = read_input()
    robot = Robot()
    machine = IntCodeMachine(memory, robot.read, robot.instruct)
    machine.run()
    print(len(robot.squares))
예제 #23
0
from aocd.models import Puzzle

from intcode import IntCodeMachine

if __name__ == '__main__':
    puzzle = Puzzle(year=2019, day=9)
    m = IntCodeMachine(list(map(int, puzzle.input_data.split(','))))
    m.set_inputs([1])
    puzzle.answer_a = next(m.run())
    m.reset()
    m.set_inputs([2])
    puzzle.answer_b = next(m.run())
예제 #24
0
def test_day_5_part_2(day_5_input):
    stack_io_handler = StackIOHandler([str(5)])
    machine = IntCodeMachine(day_5_input, io_handler=stack_io_handler)
    machine.run()
    assert stack_io_handler.io_stack[-1] == "11189491"
예제 #25
0
def test_day_5_part_1(day_5_input):
    stack_io_handler = StackIOHandler([str(1)])
    machine = IntCodeMachine(day_5_input, io_handler=stack_io_handler)
    machine.run()
    assert stack_io_handler.io_stack[-1] == "13978427"
예제 #26
0
def test_day_5_large_test(input, expected_output):
    code = "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"  # noqa: E501
    stack_io_handler = StackIOHandler([str(input)])
    machine = IntCodeMachine(code, io_handler=stack_io_handler)
    machine.run()
    assert stack_io_handler.io_stack == [str(expected_output)]
예제 #27
0
def test_day_5_jump(program, input, expected_output):
    stack_io_handler = StackIOHandler([str(input)])
    machine = IntCodeMachine(program, io_handler=stack_io_handler)
    machine.run()
    assert stack_io_handler.io_stack == [str(expected_output)]
예제 #28
0
 def testmore3(self):
     m = IntCodeMachine([109, -1, 204, 1, 99])
     assert next(m.run()) == 109
예제 #29
0
 def testmore5(self):
     m = IntCodeMachine([109, 1, 109, 9, 204, -6, 99])
     assert next(m.run()) == 204
예제 #30
0
 def testmore6(self):
     m = IntCodeMachine([109, 1, 209, -1, 204, -106, 99])
     assert next(m.run()) == 204