Example #1
0
    def test_run_program(self):
        program_list = [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50]
        answer = [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]
        cpu = intcode.Intcode(program_list)
        cpu.run_program()
        self.assertEqual(cpu.program, answer)

        program_list = [1, 0, 0, 0, 99]
        answer = [2, 0, 0, 0, 99]
        cpu = intcode.Intcode(program_list)
        cpu.run_program()
        self.assertEqual(cpu.program, answer)

        program_list = [2, 3, 0, 3, 99]
        answer = [2, 3, 0, 6, 99]
        cpu = intcode.Intcode(program_list)
        cpu.run_program()
        self.assertEqual(cpu.program, answer)

        program_list = [2, 4, 4, 5, 99, 0]
        answer = [2, 4, 4, 5, 99, 9801]
        cpu = intcode.Intcode(program_list)
        cpu.run_program()
        self.assertEqual(cpu.program, answer)

        program_list = [1, 1, 1, 4, 99, 5, 6, 0, 99]
        answer = [30, 1, 1, 4, 2, 5, 6, 0, 99]
        cpu = intcode.Intcode(program_list)
        cpu.run_program()
        self.assertEqual(cpu.program, answer)
Example #2
0
def run():
    init_memory = intcode.parse_input_to_memory("d9input.txt")
    ic = intcode.Intcode(init_memory)
    ic.set_input([1])
    ic.run_program()
    print(ic.output)

    ic = intcode.Intcode(init_memory)
    ic.set_input([2])
    ic.run_program()
    print(ic.output)
Example #3
0
def run():
    init_memory = intcode.parse_input_to_memory("d5input.txt")

    ic1 = intcode.Intcode(init_memory)
    ic1.set_input([1])
    ic1.run_program()
    print(ic1.output)

    ic2 = intcode.Intcode(init_memory)
    ic2.set_input([5])
    ic2.run_program()
    print(ic2.output)
Example #4
0
    def test_jump_if_condition(self):
        for program_input in [x - 10 for x in range(30)]:
            program_list = [
                3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9
            ]
            cpu = intcode.Intcode(program_list)
            cpu.run_program(program_input)
            self.assertEqual(cpu.output[-1], program_input != 0)

        for program_input in [x - 10 for x in range(30)]:
            program_list = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1]
            cpu = intcode.Intcode(program_list)
            cpu.run_program(program_input)
            self.assertEqual(cpu.output[-1], program_input != 0)
Example #5
0
def part2():
    max_power = 0
    for a in range(5, 10):
        for b in range(5, 10):
            if b == a: continue
            for c in range(5, 10):
                if c == b or c == a: continue
                for d in range(5, 10):
                    if d == c or d == b or d == a: continue
                    for e in range(5, 10):
                        if e == d or e == c or e == b or e == a: continue

                        inputs = get_inputs(get_input_str())

                        ampA = intcode.Intcode()
                        ampB = intcode.Intcode()
                        ampC = intcode.Intcode()
                        ampD = intcode.Intcode()
                        ampE = intcode.Intcode()

                        ampA.receive_signal(a)
                        ampA.receive_signal(0)
                        ampB.receive_signal(b)
                        ampC.receive_signal(c)
                        ampD.receive_signal(d)
                        ampE.receive_signal(e)

                        some_running = True
                        final_out = 0
                        while some_running:
                            out = ampA.run(inputs, False, True)
                            ampB.receive_signal(out)
                            out = ampB.run(inputs, False, True)
                            ampC.receive_signal(out)
                            out = ampC.run(inputs, False, True)
                            ampD.receive_signal(out)
                            out = ampD.run(inputs, False, True)
                            ampE.receive_signal(out)
                            out = ampE.run(inputs, False, True)

                            if not ampA.complete or not ampB.complete or not ampC.complete or not ampD.complete or not ampE.complete:
                                ampA.receive_signal(out)
                            else:
                                final_out = out
                                some_running = False

                        if final_out > max_power:
                            max_power = final_out

    return max_power
Example #6
0
def code1():
    with open(DATA) as f:
        strcode = f.readline().strip()
        code = intcode.parse_data(strcode)
    comp = intcode.Intcode(code)
    comp.run([1])
    print('1>', comp.outvalues)
Example #7
0
def part1():
    inputs = get_inputs(get_input_str())
    # inputs = get_inputs("109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99")
    inputs.extend([0] * 1000)
    comp = intcode.Intcode()
    comp.receive_signal(1)
    return comp.run(inputs, False, True)
def main():
    with open('input.txt') as f:
        memory = map(int, f.read().split(','))

    i = intcode.Intcode(memory, [1], [])
    i.run()
    print(i.result[-1])
Example #9
0
def run():
    init_memory = intcode.parse_input_to_memory("d2input.txt")

    ic = intcode.Intcode(init_memory)
    ic.set_noun_verb(12, 2)
    print(ic.run_program())

    for input1 in range(100):
        for input2 in range(100):
            ic = intcode.Intcode(init_memory)
            ic.set_noun_verb(input1, input2)
            output = ic.run_program()
            if output == 19690720:
                print(100 * input1 + input2)
                return
            input2 += 1
        input1 += 1
Example #10
0
def run_amplifiers(amp_program_str, phases):
    memory = list(map(int, amp_program_str.split(',')))
    inputVal = 0
    for phase in phases:
        machine = intcode.Intcode(memory)
        output = machine.run_machine([phase, inputVal])
        inputVal = output[0]
    return inputVal
Example #11
0
def test_modes(modes):
    amps = [intcode.Intcode(code) for amp in range(5)]
    amps[0].input_gen = default_input(modes[0])
    asyncio.run(amps[0].run())
    for i in range(1, len(amps)):
        amps[i].input_gen = get_input(modes[i], amps[i - 1].output[-1])
        asyncio.run(amps[i].run())
    return amps
Example #12
0
def part1():
    inputs = get_inputs()
    inputs[1] = 12
    inputs[2] = 2
    comp = intcode.Intcode()
    comp.receive_signal(1)
    result = comp.run(inputs, False, False)
    return result
Example #13
0
def run_amplifiers(memory, phases):
    power = 0
    for phase in phases:
        ic = intcode.Intcode(memory)
        ic.set_input([phase, power])
        ic.run_program()
        power = ic.output
    return power
Example #14
0
    def test_program_input_output(self):
        program_list = [3, 0, 4, 0, 99]
        program_input = 42
        cpu = intcode.Intcode(program_list)
        cpu.run_program(program_input)
        self.assertEqual(cpu.output[-1], program_input)

        program_list = [3, 0, 4, 0, 99]
        program_input = 1
        cpu = intcode.Intcode(program_list)
        cpu.run_program(program_input)
        self.assertEqual(cpu.output[-1], program_input)

        program_list = [3, 0, 4, 0, 99]
        program_input = 2
        cpu = intcode.Intcode(program_list)
        cpu.run_program(program_input)
        self.assertEqual(cpu.output[-1], program_input)
Example #15
0
def code2():
    with open(DATA) as f:
        strcode = f.readline().strip()
        code = intcode.parse_data(strcode)
    computer = intcode.Intcode(code)
    computer.verbose_output = False
    computer.code[0] = 2

    tiles = defaultdict(lambda: defaultdict(int))
    play(tiles, computer, [0], trace=False)
Example #16
0
def part1Alt():
    part1 = 0
    for phase in itertools.permutations(range(5)):
        amps = [intcode.Intcode(code, [phase[i]]) for i in range(5)]
        previous_output = 0
        for amp in amps:
            amp.input(previous_output)
            previous_output = amp.execute()
        part1 = max(part1, previous_output)
    print(part1)
Example #17
0
 def __init__(self, program_str):
     memory = list(map(int, program_str.split(',')))
     memory[0] = 2  # free play mode
     self.engine = intcode.Intcode(memory, self.get_input)
     self.ball_position = []
     self.ball_direction = []
     self.paddle_position = []
     self.board = np.zeros([4, 2])
     self.score = 0
     self.pending_updates = []
Example #18
0
def code1():
    with open(DATA) as f:
        strcode = f.readline().strip()
        code = intcode.parse_data(strcode)
    computer = intcode.Intcode(code)
    computer.verbose_output = False

    panels = defaultdict(int)
    panels = run_robot(panels, computer)
    print('1>', len(panels))
Example #19
0
def point_lit(xx, yy):
    global comp

    inputs = get_inputs(get_input_str())
    inputs.extend([0] * 100000)

    comp = intcode.Intcode()

    comp.receive_signal(xx)
    comp.receive_signal(yy)
    return comp.run(inputs.copy(), False, True)
Example #20
0
def drone_move(x,y,move='north'):
    inp = inputs[move]
    flag = intcode.Intcode(data,inp=inp)
    step_x,step_y = step[move]
    if flag == 1 or flag == 2:
        y = y+step_y
        x = x+step_x
        space[y,x] = flag
    else:
        space[y+step_y,x+step_x] = flag
    return x,y,flag
Example #21
0
def solve_part_1(code: str) -> int:
    game = intcode.Intcode(code)

    screen = defaultdict(int)
    outputs = game.run(pvector())

    for x, y, tile_id in grouper(outputs, 3):
        screen[(x, y)] = tile_id

    # draw_screen(screen)

    return sum(1 for x in screen.values() if x == 2)
Example #22
0
def try_phase_setting(code, setting):
    # setting = (a, b, c, d, e)
    amp = [None] * 5
    for ampnum in range(5):
        amp[ampnum] = intcode.Intcode(code)

    inval = 0
    for ampnum in range(5):
        phase = setting[ampnum]
        outval = amp[ampnum].run([phase, inval])
        inval = outval
    return inval
Example #23
0
def code1():
    with open(DATA) as f:
        strcode = f.readline().strip()
        code = intcode.parse_data(strcode)
    computer = intcode.Intcode(code)
    computer.verbose_output = False
    area = defaultdict(lambda: defaultdict(int))
    DFS(area, computer)
    show_area(area, 0, 0)
    i, j = find_target(area)
    BFS(area, 0, 0, i, j)
    return area, i, j
Example #24
0
def part1():
    # inputs = get_inputs("3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0")
    max_power = 0
    for a in range(0, 5):
        for b in range(0, 5):
            if b == a: continue
            for c in range(0, 5):
                if c == b or c == a: continue
                for d in range(0, 5):
                    if d == c or d == b or d == a: continue
                    for e in range(0, 5):
                        if e == d or e == c or e == b or e == a: continue

                        inputs = get_inputs(get_input_str())

                        ampA = intcode.Intcode()
                        ampB = intcode.Intcode()
                        ampC = intcode.Intcode()
                        ampD = intcode.Intcode()
                        ampE = intcode.Intcode()

                        ampA.receive_signal(a)
                        ampA.receive_signal(0)
                        out = ampA.run(inputs, False, True)
                        ampB.receive_signal(b)
                        ampB.receive_signal(out)
                        out = ampB.run(inputs, False, True)
                        ampC.receive_signal(c)
                        ampC.receive_signal(out)
                        out = ampC.run(inputs, False, True)
                        ampD.receive_signal(d)
                        ampD.receive_signal(out)
                        out = ampD.run(inputs, False, True)
                        ampE.receive_signal(e)
                        ampE.receive_signal(out)
                        out = ampE.run(inputs, False, True)
                        if out > max_power:
                            max_power = out

    return max_power
Example #25
0
def runcode(part):
    with open(DATA) as f:
        strcode = f.readline().strip()
        code = intcode.parse_data(strcode)

    input_stack = [[num] for num in range(50)]
    NAT_X, NAT_Y = None, None

    def make_input_callback(num):
        def input_callback():
            return pop_input(input_stack, num)

        return input_callback

    input_callback = [make_input_callback(num) for num in range(50)]

    computers = list()
    for num in range(50):
        computer = intcode.Intcode(code)
        computer.verbose_output = False
        computer.trace = False
        computers.append(computer)

    print(input_stack)

    while 1:
        for num in range(50):
            # print('---', num, input_stack[num])
            computers[num].run([],
                               input_callback=input_callback[num],
                               return_output=True,
                               return_input=True)
            if computers[num].returned_on == 'output':
                if len(computers[num].outvalues) >= 3:
                    destnum = computers[num].outvalues.pop(0)
                    X = computers[num].outvalues.pop(0)
                    Y = computers[num].outvalues.pop(0)
                    print(num, '-->', destnum, X, Y)
                    if destnum != 255:
                        input_stack[destnum].append(X)
                        input_stack[destnum].append(Y)
                    elif part == 1:
                        print('1>', Y)
                        return
                    else:
                        if all(not _ for _ in input_stack):
                            if Y == NAT_Y:
                                print('2>', Y)
                                return
                            NAT_x, NAT_Y = X, Y
                            input_stack[0].append(X)
                            input_stack[0].append(Y)
Example #26
0
def test_modes(modes):
    amps = [intcode.Intcode(code) for amp in range(5)]
    outputs = [[0]] + [[]]*4

    for i in range(len(amps)):
        def add_output(_output):
            outputs[i].append(_output[-1])
        amps[i].output_callback = add_output
        amps[i].input_gen = get_input(modes[i], outputs[i])

    for i in range(len(amps)):
        asyncio.run(amps[i].run())
    return amps
Example #27
0
def output_from_settings(settings, p=program):
    amp_a = intcode.Intcode(p[:], [settings[0]])
    amp_b = intcode.Intcode(p[:], [settings[1]])
    amp_c = intcode.Intcode(p[:], [settings[2]])
    amp_d = intcode.Intcode(p[:], [settings[3]])
    amp_e = intcode.Intcode(p[:], [settings[4]])

    amp_list = [amp_a, amp_b, amp_c, amp_d, amp_e]
    amp_index = 0
    current_sig = 0
    last_output = 0
    try:
        while True:
            if amp_index > 4:
                last_output = current_sig
                amp_index = 0
            current_amp = amp_list[amp_index]
            current_amp.send_input(current_sig)
            current_sig = next(current_amp)
            amp_index += 1
    except StopIteration:
        return last_output
Example #28
0
def feedback_amplifiers(memory, phases):
    amps = []
    for phase in phases:
        ic = intcode.Intcode(memory)
        amps.append(ic)
        ic.set_input([phase])
    power = 0
    while not amps[-1].is_halted():
        for amp in amps:
            amp.input.append(power)
            amp.run_to_output()
            power = amp.output
    return power
Example #29
0
def main():
    part2 = 0
    for phase in itertools.permutations(range(5, 10)):
        amps = [intcode.Intcode(code, [phase[i]]) for i in range(5)]
        previous_output = 0
        while all(not amp.halted for amp in amps):
            for amp in amps:
                amp.input(previous_output)
                output = amp.run()
                if not amp.halted:
                    previous_output = output
        part2 = max(part2, previous_output)
    print(part2)
Example #30
0
def code1():
    with open(DATA) as f:
        strcode = f.readline().strip()
        code = intcode.parse_data(strcode)

    computer = intcode.Intcode(code)
    computer.verbose_output = False
    computer.trace = False

    for line in INIT_SEQ.splitlines():
        computer.invalues.extend([ord(c) for c in line.strip()] + [10])

    items_combinations = list()
    for n in range(1, len(ITEMS) + 1):
        items_combinations.extend(list(itertools.combinations(ITEMS, n)))

    def input_callback():
        if computer.outvalues:
            print(''.join(chr(_) for _ in computer.outvalues))
            computer.outvalues.clear()

        if not computer.invalues:
            if items_combinations:
                items = items_combinations.pop(0)
                test_items(computer, items)
            else:
                inp = input()
                if inp == 'end':
                    exit()
                if inp == 'n':
                    inp = 'north'
                if inp == 'e':
                    inp = 'east'
                if inp == 's':
                    inp = 'south'
                if inp == 'w':
                    inp = 'west'
                computer.invalues.extend([ord(c) for c in inp] + [10])

        return computer.invalues.pop(0)

    while 1:
        computer.run([],
                     input_callback=input_callback,
                     return_output=False,
                     return_input=True)
        if computer.returned_on == 'terminate':
            print(''.join(chr(_) for _ in computer.outvalues))
            break