Example #1
0
def main():
    # part 1
    memory_ = numpy.loadtxt('day9\input.txt', delimiter=',', dtype=numpy.int64)
    #memory_ = numpy.fromstring('1102,34915192,34915192,7,4,7,99,0', sep=',', dtype=numpy.int64)
    #memory_ = numpy.fromstring('104,1125899906842624,99', sep=',', dtype=numpy.int64)
    #memory_ = numpy.fromstring('109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99', sep=',', dtype=numpy.int64)

    ### Part 1
    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    cpu.queue_input(1)
    cpu.exec()
    print(f'Part 1 output:')
    try:
        while True:
            print(str(cpu.get_output(False)) + ' ', )
    except Halted:
        print('done')

    ### Part 2
    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    cpu.queue_input(2)
    cpu.exec()
    print(f'Part 2 output:')
    try:
        while True:
            print(str(cpu.get_output(False)) + ' ', )
    except Halted:
        print('done')
Example #2
0
def main():
    # part 1
    memory_ = numpy.loadtxt(r'day17\input.txt',
                            delimiter=',',
                            dtype=numpy.int64)
    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    cpu.background_exec()

    grid = defaultdict(lambda: ord('.'))
    (x, y) = (0, 0)

    bot_loc = (0, 0)

    while True:
        try:
            val = cpu.get_output(block=True)
            if val == 10:
                # new row
                x = 0
                y += 1
                #print_grid(grid)
                continue
            if val == ord('^'):
                bot_loc = (x, y)
            grid[(x, y)] = val
            x += 1
        except Halted:
            print('Program Halted')
            break

    print(f'Part 1: {sum( x*y for (x,y) in find_intersections(grid))}')

    ### part 2:
    sub_a = 'R,8,L,4,R,4,R,10,R,8\n'
    sub_b = 'L,12,L,12,R,8,R,8\n'
    sub_c = 'R,10,R,4,R,4\n'
    main_routine = 'A,A,B,C,B,C,B,C,C,A\n'

    memory = numpy.array(memory_, copy=True)
    memory[0] = 2  # set to override mode
    cpu = CPU(memory)

    # queue inputs
    for c in main_routine + sub_a + sub_b + sub_c + 'n\n':
        cpu.queue_input(ord(c))

    # run
    cpu.exec()

    while True:
        try:
            space_dust = cpu.get_output()
        except (Empty, Halted):
            break
    print(f'Part 2: {space_dust}')
Example #3
0
def main():
    # part 1
    print('Part 1:')
    memory_ = numpy.loadtxt('day5\input.txt', delimiter=',', dtype=numpy.int64)
    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    cpu.queue_input(1)
    cpu.exec()

    # part 2
    print('=============================================\nPart 2')
    memory_ = numpy.loadtxt('day5\input.txt', delimiter=',', dtype=numpy.int64)
    memory = numpy.array(memory_, copy=True)
    #test = [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]
    #memory = numpy.array(test, dtype=numpy.int64)
    cpu = CPU(memory)
    cpu.queue_input(5)
    cpu.exec()
Example #4
0
def main():
    # part 1
    memory_ = numpy.loadtxt('day7\input.txt', delimiter=',', dtype=numpy.int64)
    ##memory_ = numpy.fromstring('3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0', sep=',', dtype=numpy.int64)
    ##memory_ = numpy.fromstring('3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0', sep=',', dtype=numpy.int64)

    max_out = None
    max_seq = None
    for phase_seq in itertools.permutations(range(5), 5):
        last_output = 0
        for phase in phase_seq:
            memory = numpy.array(memory_, copy=True)
            cpu = CPU(memory)
            cpu.queue_input(phase)
            cpu.queue_input(last_output)
            cpu.exec()
            last_output = cpu.get_output()
        if not max_out or (last_output > max_out):
            max_out = last_output
            max_seq = phase_seq

    print(f'Part 1: Max: {max_out}')
    print(f'Part 1: Max Seq: {max_seq}')

    # 3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5
    # part 2
    #memory_ = numpy.fromstring('3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5', sep=',', dtype=numpy.int64)

    max_out = None
    max_seq = None
    for phase_seq in itertools.permutations(range(5, 10), 5):
        cpu_a = CPU(numpy.array(memory_, copy=True), input_block=True)
        cpu_b = CPU(numpy.array(memory_, copy=True),
                    input_block=True,
                    input_q=cpu_a.output_q)
        cpu_c = CPU(numpy.array(memory_, copy=True),
                    input_block=True,
                    input_q=cpu_b.output_q)
        cpu_d = CPU(numpy.array(memory_, copy=True),
                    input_block=True,
                    input_q=cpu_c.output_q)
        cpu_e = CPU(numpy.array(memory_, copy=True),
                    input_block=True,
                    input_q=cpu_d.output_q)

        cpu_a.input_q = cpu_e.output_q

        cpu_a.queue_input(phase_seq[0])
        cpu_a.queue_input(0)
        cpu_b.queue_input(phase_seq[1])
        cpu_c.queue_input(phase_seq[2])
        cpu_d.queue_input(phase_seq[3])
        cpu_e.queue_input(phase_seq[4])
        # start CPU's in threads
        a_thread = threading.Thread(target=cpu_a.exec)
        b_thread = threading.Thread(target=cpu_b.exec)
        c_thread = threading.Thread(target=cpu_c.exec)
        d_thread = threading.Thread(target=cpu_d.exec)
        e_thread = threading.Thread(target=cpu_e.exec)

        a_thread.start()
        b_thread.start()
        c_thread.start()
        d_thread.start()
        e_thread.start()

        e_thread.join()
        last_output = cpu_e.get_output()
        if not max_out or (last_output > max_out):
            max_out = last_output
            max_seq = phase_seq

    print(f'Part 2 max out: {max_out}')