Beispiel #1
0
def main():
    # part 1
    memory_ = numpy.loadtxt('day15\input.txt', delimiter=',', dtype=numpy.int64)
    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    cpu.background_exec()

    maze = defaultdict(int)
    (x, y) = (0, 0)
    facing = N
    left = None
    right = None
    fwd = None
    maze[(x, y)] = EMPTY

    root_node = MazeNode()

    root_thread = threading.Thread(target=maze_worker, args=(cpu, facing, maze, root_node, (x, y)))
    root_thread.start()

    first = True
    if do_plot:
        while True:
            task = plot_q.get()
            if task:
                args, kwargs = task
                plt.imshow(*args, **kwargs)
                #plt.pause(0.0001)
                if first:
                    plt.show(block=False)
                    first == False
                else:
                    plt.draw()
                time.sleep(0)
            else:
                break
                print('plot kill')

    root_thread.join()
    test = target_node
    steps = 0
    while test:
        steps += test.length
        test = test.parent
    print(f'part 1: {steps}')
    plot(maze, 0, 0, show=True)

    print(target_node)

    ### part 2
    print(f'Part 2: {fill_up(target_node)}')
Beispiel #2
0
def main():
    # part 1
    memory_ = numpy.loadtxt('day11\input.txt',
                            delimiter=',',
                            dtype=numpy.int64)

    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    cpu.background_exec()

    (x, y) = (0, 0)
    panels = defaultdict(lambda: BLACK)
    facing = up
    loop_count = 0
    panels[(x, y)] = BLACK
    while (not cpu.done):  # and (len(panels) < 400):
        #print(f'input: {panels[(x,y)]}')
        loop_count += 1
        if not loop_count % 1000:
            print(loop_count)
        try:
            cpu.queue_input(panels[(x, y)])
            color = cpu.get_output(block=True)
            #print(f'{color}')
            direction = cpu.get_output(block=True)
            #print(f'{direction}')
        except Halted:
            print('Program done')
            break

        panels[(x, y)] = color

        # rotate
        facing = facing.rotate(direction)

        # move forward
        if facing is up:
            y -= 1
        elif facing is right:
            x += 1
        elif facing is down:
            y += 1
        elif facing is left:
            x -= 1

    #print(f'Part 1: {len(panels)}')
    min_x = min(panels, key=lambda p: p[0])[0]
    max_x = max(panels, key=lambda p: p[0])[0]
    min_y = min(panels, key=lambda p: p[1])[1]
    max_y = max(panels, key=lambda p: p[1])[1]

    img = numpy.zeros((max_y + 1 - min_y, max_x + 1 - min_x),
                      dtype=numpy.uint8)
    for (panel, color) in panels.items():
        img[panel[1], panel[0]] = color
    #plt.imshow(img)
    #plt.show()
    print(panels)
Beispiel #3
0
def y2021_d7():
    print('2021 Day 7 Easter Egg: run input as intcode program')
    memory_ = numpy.loadtxt('y2021\day7\input.txt',
                            delimiter=',',
                            dtype=numpy.int64)

    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    cpu.background_exec()

    try:
        while True:
            print(chr(cpu.get_output(block=True)), end='')
    except Halted:
        pass
    print('\nDone')
Beispiel #4
0
def main():
    # part 1
    memory_ = numpy.loadtxt(r'day19\input_mine.txt', delimiter=',', dtype=numpy.int64)
    # darkgray answer: 379 981
    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    grid = defaultdict(int)
    for x in range(100):
        for y in range(100):
            grid[(x,y)] = get_point(cpu, x, y)
            #print(f'{(x, y)}: {val}')
    print_grid(grid)
    count = sum(v for v in grid.values())
    print(f'Part 1: {count}')

    ##### part 2
    yprime = 99
    xprime = 99

    x = 0
    y = 0

    bl = 0
    tr = 0

    while True:
        bl = get_point(cpu, x, y+yprime)
        tr = get_point(cpu, x+xprime, y)

        if bl and tr:
            print(f'Part 2: {(x, y)}')
            break
        # iterate x
        while bl == 0:
            x += 1
            bl = get_point(cpu, x, y+yprime)
        # iterate y
        while tr == 0:
            y += 1
            tr = get_point(cpu, x+xprime, y)
Beispiel #5
0
def main():
    # part 1
    memory_ = numpy.loadtxt('day13\input.txt',
                            delimiter=',',
                            dtype=numpy.int64)
    memory = numpy.array(memory_, copy=True)
    cpu = CPU(memory)
    cpu.background_exec()

    screen = defaultdict(lambda: 0)

    while True:
        try:
            x = cpu.get_output(block=True)
            y = cpu.get_output(block=True)
            val = cpu.get_output(block=True)
            screen[(x, y)] = val
            print(f'({x}, {y}) {val}')
        except Halted:
            print('halted')
            break

    blocks = len([i for i in screen.values() if i == BLOCK])
    print(f'Part 1: {blocks}')

    memory = numpy.array(memory_, copy=True)
    memory[0] = 2
    # hack to make paddle the whole width
    memory[1608:1652] = 3

    cpu = CPU(memory)
    for i in range(10000):
        cpu.queue_input(0)
    screen = defaultdict(lambda: 0)

    cpu.background_exec()
    score = 0
    while True:
        try:
            x = cpu.get_output(block=True)
            y = cpu.get_output(block=True)
            val = cpu.get_output(block=True)
            if (x == -1) and (y == 0):
                score = val
            #screen[(x,y)] = val
            #print(f'({x}, {y}) {val}')
        except Halted:
            print('halted')
            break

    print(f'Part 2: {score}')
Beispiel #6
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')
Beispiel #7
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()
Beispiel #8
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}')