Exemple #1
0
    def test_bfs_to_graph(self):
        the_map = [
            "########################",
            "#[email protected].#",
            "######################.#",
            "#d.....................#",
            "########################"
        ]
        m = Matrix()
        targets = []
        m.init_from_string('\n'.join(the_map))
        for x, y in m.index_range():
            name = m.get(x, y)
            if name in string.ascii_lowercase:
                targets.append(Target(x, y, name))

        def expand(x, y):
            x_expand = list(map(lambda x: (x, y), filter(lambda x: x >= 0 and x < m.width, [x + 1, x - 1])))
            y_expand = list(map(lambda y: (x, y), filter(lambda x: x >= 0 and x < m.height, [y + 1, y - 1])))
            return list(filter(lambda p: m.get(*p) != "#", x_expand + y_expand))

        g = bfs_to_graph(targets, expand)

        self.assertEqual(10, g.distance('b', 'c'))
        self.assertEqual(44, g.distance('d', 'f'))
        self.assertEqual(len(targets), len(g.__neighbours))
Exemple #2
0
def map_to_matrix(the_map: List[str]) -> Matrix:
    m = Matrix()
    m.init_from_string('\n'.join(the_map))
    return m
    graph = bfs_to_graph(ps, maze_expand)

    # add teleportation
    for name in list(map(lambda n: n.name, ps)):
        alt_name = name + "X"
        if graph.has_node(alt_name):
            graph.add_node(name, alt_name, 1)
            graph.add_node(alt_name, name, 1)

    return graph


if __name__ == "__main__":
    maze = util.read_input("day20", should_strip=False)
    m = Matrix()
    m.init_from_string('\n'.join(maze))
    ps = portals(m)

    g = construct_graph(ps, m)
    min_distance = dijkstra(g, "AA", "ZZ")

    print("Part Onw: ", min_distance)

# --- Part Two ---
#
# Strangely, the exit isn't open when you reach it. Then, you remember: the ancient Plutonians were famous for building recursive spaces.
#
# The marked connections in the maze aren't portals: they physically connect to a larger or smaller copy of the maze. Specifically, the labeled tiles around the inside edge actually connect to a smaller copy of the same maze, and the smaller copy's inner labeled tiles connect to yet a smaller copy, and so on.
#
# When you enter the maze, you are at the outermost level; when at the outermost level, only the outer labels AA and ZZ function (as the start and end, respectively); all other outer labeled tiles are effectively walls. At any other level, AA and ZZ count as walls, but the other outer labeled tiles bring you one level outward.
#