Exemple #1
0
def part2():
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, "input.txt")
    vm = intcode.VM()
    vm.load_file(filename)
    vm.write(0, 2, 1)  # put 2 quarters into machine
    screen = dict()
    score = 0
    while True:
        status = vm.step()
        if len(vm.outputs) == 3:
            x, y, t = vm.outputs
            screen[(x, y)] = t
            vm.outputs = []
            if x == -1:
                score = t
            if t == 3:
                paddle_x = x
            if t == 4:
                ball_x = x
        if status == vm.WAIT:
            vm.inputs.append(np.sign(ball_x - paddle_x))
        if status == vm.HALT:
            break
    return score
Exemple #2
0
 def __init__(self, program):
     self.program = program
     self.computer = intcode.VM()
     self.computer.load(program)
     self.direction = 0
     # north
     self.dxdy = {0: (0, 1), 1: (1, 0), 2: (0, -1), 3: (-1, 0)}
     self.x = 0
     self.y = 0
Exemple #3
0
def init_vms():
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, "input.txt")
    vm = intcode.VM()
    vm.load_file(filename)
    vms = [vm.copy() for i in range(50)]
    for i, vm in enumerate(vms):
        vm.inputs.append(i)
        vm.run()
    return vms
Exemple #4
0
def part1():
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, "input.txt")
    vm = intcode.VM()
    vm.load_file(filename)
    screen = dict()
    for x in range(50):
        for y in range(50):
            outputs = vm.copy().run([x, y])
            screen[(x, y)] = outputs[-1]
    print(sum(c == 1 for (x, y), c in screen.items()))
    print_screen(screen)
Exemple #5
0
def part1():
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, "input.txt")
    vm = intcode.VM()
    vm.load_file(filename)
    screen = dict()
    while True:
        status = vm.step()
        if len(vm.outputs) == 3:
            x, y, t = vm.outputs
            screen[(x, y)] = t
            vm.outputs = []
        if status == vm.HALT:
            break
    return sum(v == 2 for k, v in screen.items())
Exemple #6
0
def part2():
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, "input.txt")
    vm = intcode.VM()
    vm.load_file(filename)
    x = 100
    y = 100
    # get into the beam
    while get_value(vm, x, y) == 0:
        x += 1
    while True:
        # check if box of size 100x100 fits
        if get_value(vm, x + 99, y - 99) == 1:
            print(10000 * x + (y - 99))
            break
        # move down, then right until we find 1
        y += 1
        while get_value(vm, x, y) == 0:
            x += 1
Exemple #7
0
def animate(stdscr):
    ROWS = 22 + 2
    COLS = 44 + 2
    win = curses.newwin(ROWS, COLS)
    curses.noecho()
    curses.cbreak()
    curses.curs_set(0)
    vm = intcode.VM()
    vm.load_file("input.txt")
    vm.write(0, 2, 1)  # put quarter into machine
    screen = dict()
    score = 0
    charcode = {0: " ", 1: "█", 2: "▢", 3: "▁", 4: "●"}
    while True:
        status = vm.step()
        if len(vm.outputs) == 3:
            x, y, t = vm.outputs
            screen[(x, y)] = t
            vm.outputs = []
            if x == -1:
                score = t
            if t == 3:
                paddle_x = x
            if t == 4:
                ball_x = x
        if status == vm.WAIT:
            vm.inputs.append(np.sign(ball_x - paddle_x))
            win.clear()
            for (x, y) in screen:
                if x < 0:
                    continue
                win.addch(y + 1, x, charcode[screen[x, y]])
            win.addstr(0, 0, f"SCORE: {score}")
            win.refresh()
            time.sleep(0.015)
        if status == vm.HALT:
            break
    # Clean up before exiting
    curses.nocbreak()
    win.keypad(0)
    curses.echo()
    curses.endwin()
Exemple #8
0
def part1(filename):

    vm = intcode.VM()
    vm.load_file(filename)

    start = (0, 0)
    maze = defaultdict(lambda: UNEXPLORED)
    maze[start] = PATH
    explore(start, maze, vm)

    target = [n for n in maze if maze[n] == TARGET][0]
    print(f"Found Oxygen System at {target}")

    shortest_path = shortest_path_bfs(maze, start, target)
    print(f"Shortest path {len(shortest_path)}")
    draw_maze(maze, shortest_path)

    distances = get_distances(maze, target)
    print(f"Max distance from OS: {max(distances.values())}")
    plot_distances(maze, distances)
Exemple #9
0
def get_intersections(image):
    intersections = set()
    for (x, y) in image:
        neighbors = [(x, y), (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
        if all(value == SCAFFOLD
               for value in [image.get(n, 0) for n in neighbors]):
            intersections.add((x, y))
    return intersections


dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, "input.txt")

# part 1

vm = intcode.VM()
vm.load_file(filename)
outputs = vm.run([])
image = get_image(outputs)
print_image(image)
intersections = get_intersections(image)
print(sum(x * y for x, y in intersections))

# part 2

NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3