示例#1
0
def getInput(path):
    with open(path, 'r') as file:
        lines = [line.strip() for line in file.readlines()]

    uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    lowercase = 'abcdefghijklmnopqrstuvwxyz'

    graph = Graph()
    doors = {}
    keys = {}
    origins = []
    pos = Vector(0, 0)

    for line in lines:
        for c in line:
            if c == '.':
                pass
            elif c in uppercase:
                doors[c] = pos
            elif c in lowercase:
                keys[c] = pos
            elif c == '@':
                origins.append(pos)

            if c != '#':
                graph.add_node(pos, c)
            pos = pos.right()
        pos = pos.down()
        pos.x = 0

    graph.auto_link_manhattan()

    return graph, doors, keys, origins
示例#2
0
def getInput1():
    with open('day24.txt', 'r') as file:
        lines = [line.strip() for line in file.readlines()]
    pos = Vector(0, 0, 0)
    grid = {}
    for line in lines:
        for c in line:
            grid[pos] = c
            pos = pos.right()
        pos = pos.down()
        pos.x = 0
    return grid
示例#3
0
def addEmptyGrid(grid, level):
    for y in range(5):
        for x in range(5):
            if x == y == 2:
                continue
            grid[Vector(x, y, level)] = NOT_BUG
    return grid
示例#4
0
def checkLevel(grid, upper, lower):
    addUpper = False
    addLower = False
    for y in range(5):
        for x in range(5):
            if x == y == 2:
                continue
            if grid[Vector(x, y, upper)] == BUG:
                addUpper = True
            if grid[Vector(x, y, lower)] == BUG:
                addLower = True
    if addUpper:
        addEmptyGrid(grid, upper + 1)
        upper += 1
    if addLower:
        addEmptyGrid(grid, lower - 1)
        lower -= 1
    return upper, lower
示例#5
0
def countNeighbors2(grid, pos):
    count = 0
    for neighbor in (pos.up(), pos.left(), pos.down(), pos.right()):
        if neighbor in grid:
            if grid[neighbor] == BUG:
                count += 1

        if neighbor.y == -1:
            p = Vector(2, 1, neighbor.z + 1)
            if p in grid and grid[p] == BUG:
                count += 1
        elif neighbor.y == 5:
            p = Vector(2, 3, neighbor.z + 1)
            if p in grid and grid[p] == BUG:
                count += 1
        if neighbor.x == -1:
            p = Vector(1, 2, neighbor.z + 1)
            if p in grid and grid[p] == BUG:
                count += 1
        elif neighbor.x == 5:
            p = Vector(3, 2, neighbor.z + 1)
            if p in grid and grid[p] == BUG:
                count += 1

    if pos.x == 1 and pos.y == 2:
        for asdf in range(5):
            neighbor = Vector(0, asdf, pos.z - 1)
            if neighbor in grid and grid[neighbor] == BUG:
                count += 1
    if pos.x == 3 and pos.y == 2:
        for asdf in range(5):
            neighbor = Vector(4, asdf, pos.z - 1)
            if neighbor in grid and grid[neighbor] == BUG:
                count += 1

    if pos.x == 2 and pos.y == 1:
        for asdf in range(5):
            neighbor = Vector(asdf, 0, pos.z - 1)
            if neighbor in grid and grid[neighbor] == BUG:
                count += 1
    if pos.x == 2 and pos.y == 3:
        for asdf in range(5):
            neighbor = Vector(asdf, 4, pos.z - 1)
            if neighbor in grid and grid[neighbor] == BUG:
                count += 1

    return count
示例#6
0
def getInput2():
    with open('day20.txt', 'r') as file:
        lines = file.readlines()

    uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    # 30 should be enough. 25 is not enough
    depth = 30

    print("adding nodes...")
    graph = Graph()
    letters = {}
    pos = Vector(0, 0, 0)
    for line in lines:
        for c in line:
            if c == '.':
                dpos = pos.clone()
                for i in range(depth):
                    dpos.z = i
                    graph.add_node(dpos.clone())

            elif c in uppercase:
                letters[pos] = c

            pos = pos.right()

        pos = pos.down()
        pos.x = 0

    print("linking nodes...")
    graph.auto_link_manhattan()

    portals = {}
    for pos in letters:
        if pos.up() in letters:
            moniker = letters[pos.up()] + letters[pos]
            if pos.down() in graph.nodes:
                newPortal = pos.down()
            elif pos.up().up() in graph.nodes:
                newPortal = pos.up().up()
            else:
                assert False
            if moniker not in portals:
                portals[moniker] = newPortal
            else:
                if isInner(portals[moniker]):
                    assert isInner(newPortal) is False
                    portals[moniker] = (newPortal, portals[moniker])
                else:
                    assert isInner(newPortal)
                    portals[moniker] = (portals[moniker], newPortal)

        elif pos.left() in letters:
            moniker = letters[pos.left()] + letters[pos]
            if pos.right() in graph.nodes:
                newPortal = pos.right()
            elif pos.left().left() in graph.nodes:
                newPortal = pos.left().left()
            else:
                assert False
            if moniker not in portals:
                portals[moniker] = newPortal
            else:
                if isInner(portals[moniker]):
                    assert isInner(newPortal) is False
                    portals[moniker] = (newPortal, portals[moniker])
                else:
                    assert isInner(newPortal)
                    portals[moniker] = (portals[moniker], newPortal)

    print("linking portals...")
    for port in portals:
        if port == 'AA' or port == 'ZZ':
            continue
        for i in range(depth - 1):
            # connect inner to outer
            inner = portals[port][1].clone()  # inner, this level
            inner.z = i
            outer = portals[port][0].clone()  # outer, this level
            outer.z = i + 1
            graph.add_edge(inner, outer, directed=False)

    return graph, portals
示例#7
0
def getInput():
    with open('day20.txt', 'r') as file:
        lines = file.readlines()

    uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    letters = {}
    graph = Graph()
    pos = Vector(0, 0)
    for line in lines:
        for c in line:
            if c == '.':
                graph.add_node(pos)
            elif c in uppercase:
                letters[pos] = c

            pos = pos.right()

        pos = pos.down()
        pos.x = 0

    graph.auto_link_manhattan()

    portals = {}
    for pos in letters:
        if pos.up() in letters:
            moniker = letters[pos.up()] + letters[pos]
            if pos.down() in graph.nodes:
                newPortal = pos.down()
            elif pos.up().up() in graph.nodes:
                newPortal = pos.up().up()
            else:
                assert False
            if moniker not in portals:
                portals[moniker] = newPortal
            else:
                portals[moniker] = (portals[moniker], newPortal)
        elif pos.left() in letters:
            moniker = letters[pos.left()] + letters[pos]
            if pos.right() in graph.nodes:
                newPortal = pos.right()
            elif pos.left().left() in graph.nodes:
                newPortal = pos.left().left()
            else:
                assert False
            if moniker not in portals:
                portals[moniker] = newPortal
            else:
                portals[moniker] = (portals[moniker], newPortal)

    for port in portals:
        if port == 'AA' or port == 'ZZ':
            continue
        graph.add_edge(portals[port][0], portals[port][1], directed=False)

    return graph, portals