Exemple #1
0
def part1(data):
    print(data)

    grid = []
    size = 50
    for j in range(size):
        row = []
        for i in range(size):
            row.append(".")
        grid.append(row)

    for j in range(size):
        for i in range(size):
            print("inputs", i, j)
            # in this problem, the intcoder only runs once for each input
            intcoder = Intcode(deepcopy(data), [i, j], debug=False)
            intcoder.run()
            print("output", intcoder.output)
            if intcoder.output[0] == 1:
                grid[j][i] = "#"

    print_2d_grid(grid)
    count = 0
    for row in grid:
        for item in row:
            if item == "#":
                count += 1

    print("count", count)
Exemple #2
0
def part2(data):

    asciiInput = ""

    # PART 1
    # if not (A or B or C) and D then jump
    # J if A is ground
    asciiInput += "NOT A J\n"
    # T if B is ground
    asciiInput += "NOT B T\n"

    # J if A or B is ground
    asciiInput += "OR T J\n"
    # T if C is ground
    asciiInput += "NOT C T\n"
    # J if (A or B or C) is ground
    asciiInput += "OR T J\n"

    # J if (A or B or C) and D is ground
    asciiInput += "AND D J\n"

    # PART 2
    # jump if D and (H or EI or EF)
    #         D and (H or (E and (I or F)))
    # set T = I, T OR F, T AND E, T OR H
    asciiInput += "NOT I T\n"
    asciiInput += "NOT I T\n"
    asciiInput += "OR F T\n"
    asciiInput += "AND E T\n"
    asciiInput += "OR H T\n"
    # huh....
    asciiInput += "OR E T\n"
    asciiInput += "OR H T\n"
    asciiInput += "AND T J\n"

    # final command
    # asciiInput += "WALK\n"
    asciiInput += "RUN\n"

    intcode_input = asciiToIntcodeInput(asciiInput)
    intcoder = Intcode(data, intcode_input)
    intcoder.run()

    # check intocder output
    if len(intcoder.output) > 0:
        if intcoder.output[-1] > 127:
            print("hull damage:", intcoder.output[-1])
        else:
            print("droid fell")
            print_2d_grid(intcodeOutputToAscii(intcoder.output))
            print()
    else:
        print("ERROR: no intcoder output??")
Exemple #3
0
def part1(data):
    print(data)

    intcode_input = []
    intcoder = Intcode(data, intcode_input, extra_mem=10000, debug=False)
    intcoder.run()
    output = intcoder.output
    print(output)
    grid = intcodeOutputToAscii(output)
    print_2d_grid(grid)
    intersections = findIntersections(grid)
    print("intersections:", intersections)
    total = 0
    for (x, y) in intersections:
        total += x * y
    print("total", total)
Exemple #4
0
def part1(grid):
    print_2d_grid(grid)
    doors = findDoors(grid)
    print("doors", doors)

    start = end = None
    for door in doors:
        if door[0] == "AA":
            start = door
        elif door[0] == "ZZ":
            end = door

    # construct a graph where
    #  - each door is a node
    #  - edges between different doors are weighted as the number of steps
    #  - edges between same doors are weight 0
    G = nx.Graph()
    # add nodes
    for door, coord in doors:
        G.add_node((door, coord))

    # add edges between different doors
    for pair in getPairs(doors):
        door, coord = pair[0]
        door2, coord2 = pair[1]
        if coord != coord2 and not G.has_edge(door2, door):
            if door != door2:
                path = bfs_2d_grid(grid, coord, coord2, include=[emptyChar])
                if path is not None:
                    w = len(path) - 1
                    print("adding", w, "edge", (door, coord), (door2, coord2))
                    G.add_edge((door, coord), (door2, coord2), weight=w)
            else:  # same door, so it's a free portal
                print("adding 0 edge", (door, coord), (door2, coord2))
                G.add_edge((door, coord), (door2, coord2), weight=1)
    res = nx.shortest_path_length(G, start, end, weight='weight')
    print("answer", res)
Exemple #5
0
def part1(data):
    asciiInput = ""

    # GOAL: if not (A or B or C) and D then jump

    # J if A is ground
    asciiInput += "NOT A J\n"
    # T if B is ground
    asciiInput += "NOT B T\n"
    # J if A or B is ground
    asciiInput += "OR T J\n"

    # T if C is ground
    asciiInput += "NOT C T\n"
    # J if (A or B or C) is ground
    asciiInput += "OR T J\n"

    # J if (A or B or C) and D is ground
    asciiInput += "AND D J\n"

    # final command
    asciiInput += "WALK\n"

    intcode_input = asciiToIntcodeInput(asciiInput)
    intcoder = Intcode(data, intcode_input)
    intcoder.run()

    # check intocder output
    if len(intcoder.output) > 0:
        if intcoder.output[-1] > 127:
            print("hull damage:", intcoder.output[-1])
        else:
            print("droid fell")
            print_2d_grid(intcodeOutputToAscii(intcoder.output))
            print()
    else:
        print("ERROR: no intcoder output??")
Exemple #6
0
def part1(board):
    print_2d_grid(board)
    prevBoards = [deepcopy(board)]
    numIters = 1
    while True:
        board = oneMinute(board)
        # check previous boards
        for prevBoard in prevBoards:
            if board == prevBoard:
                print("SAME")
                print_2d_grid(board)
                print("answer:",calcBio(board))
                exit(0)
        print(numIters)
        numIters += 1
        print_2d_grid(board)
        print("")
        prevBoards.append(deepcopy(board))
Exemple #7
0
def part2(grid):
    print_2d_grid(grid)
    doors = findDoors(grid)
    num_layers = 30

    # distinguish between inner and outer doors
    newdoors = []
    for door, coord in doors:
        x, y = coord
        # outer door
        if y == 2 or x == 2 or y == 116 or x == 118:
            newdoors.append((door, coord, True))
        else:
            newdoors.append((door, coord, False))
    doors = newdoors
    print("doors", doors)

    # set start and end
    start = end = None
    for door in doors:
        if door[0] == "AA":
            start = (0, door[0], door[1], door[2])
        elif door[0] == "ZZ":
            end = (0, door[0], door[1], door[2])

    # construct a graph where
    # for each "layer":
    #  - each door is a node
    #  - edges between different doors are weighted as the number of steps
    #  - edges between same doors in the different layers are weight 1
    G = nx.Graph()

    # for each layer, add doors and edges within that layer
    for i in range(num_layers):
        # add nodes
        for door, coord, isOuter in doors:
            # only add these to the first layer
            if door == "AA" or door == "ZZ":
                if i == 0:
                    G.add_node((i, door, coord, isOuter))
                    print("add node", (i, door, coord, isOuter))
                    continue
                else:
                    continue
            # for the first layer, don't add outer doors
            if isOuter and i == 0:
                continue
            # for the last layer, don't add inner doors
            if not isOuter and i == num_layers - 1:
                continue
            G.add_node((i, door, coord, isOuter))
            print("add node", (i, door, coord, isOuter))

        # add edges between different doors in this layer
        for pair in getPairs(doors):
            door, coord, isOuter = pair[0]
            door2, coord2, isOuter2 = pair[1]
            u = (i, door, coord, isOuter)
            v = (i, door2, coord2, isOuter2)
            # make sure we have both nodes (e.g. not outer for first layer)
            if G.has_node(u) and G.has_node(v):
                if door != door2 and coord != coord2 and not G.has_edge(u, v):
                    path = bfs_2d_grid(grid,
                                       coord,
                                       coord2,
                                       include=[emptyChar])
                    if path is not None:
                        w = len(path) - 1
                        print("adding", w, "edge", u, v)
                        G.add_edge(u, v, weight=w)

    # add edges between layers....
    # connect inner to outer
    for i in range(num_layers - 1):
        for door, coord, isOuter in doors:
            for door2, coord2, isOuter2 in doors:
                if door == door2 and coord != coord2 and not isOuter:
                    G.add_edge((i, door, coord, isOuter),
                               (i + 1, door2, coord2, isOuter2),
                               weight=1)
                    print("adding", 1, "layer jump", (i, door, coord, isOuter),
                          (i + 1, door2, coord2, isOuter2))

    # Find the shortest path
    res = nx.shortest_path_length(G, start, end, weight='weight')
    print("answer", res)
Exemple #8
0
def printLayers(layers):
    for i in sorted(layers.keys()):
        print("Layer",i)
        print_2d_grid(layers[i])
        print()