Ejemplo n.º 1
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)
Ejemplo n.º 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}')
Ejemplo n.º 3
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}')
Ejemplo n.º 4
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)}')
Ejemplo n.º 5
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')