Exemplo n.º 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))
Exemplo n.º 2
0
def map_to_matrix(the_map: List[str]) -> Matrix:
    m = Matrix()
    m.init_from_string('\n'.join(the_map))
    return m
Exemplo n.º 3
0
    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.
Exemplo n.º 4
0
#     The bottom-middle intersection is 6 from the left and 4 from the top, so its alignment parameter is 24.
#     The bottom-right intersection's alignment parameter is 40.
#
# To calibrate the cameras, you need the sum of the alignment parameters. In the above example, this is 76.
#
# Run your ASCII program. What is the sum of the alignment parameters for the scaffold intersections?


SCAFFOLDING = "#"

program = list(map(int, read_input("day17", ",")))
computer = IntcodeComputer(program)
computer.run()
output = ''.join(list(map(chr, computer.output_list)))
elements = list(map(list, output.split("\n")))
matrix = Matrix()
matrix.init_from_values(elements)
# matrix.print_top_left(print_headers=True)
x_points = 0
for x, y in matrix.index_range():
    if 0 < x < matrix.width and 0 < y < matrix.height and matrix.get(x, y) == SCAFFOLDING:
        if matrix.get(x - 1, y) == SCAFFOLDING and \
            matrix.get(x + 1, y) == SCAFFOLDING and \
            matrix.get(x, y - 1) == SCAFFOLDING and \
            matrix.get(x, y + 1) == SCAFFOLDING:
            x_points += x * y

print("Part One:", x_points)


# --- Part Two ---
Exemplo n.º 5
0
def to_matrix(lines):
    return Matrix().init_from_values(list(map(list, lines)))
Exemplo n.º 6
0
#
# In this example, the number of points affected by the tractor beam in the 10x10 area closest to the emitter is 27.
#
# However, you'll need to scan a larger area to understand the shape of the beam. How many points are affected by the tractor beam in the 50x50 area closest to the emitter? (For each of X and Y, this will be 0 through 49.)


def run_program(mem, x, y):
    comp = IntcodeComputer(mem)
    comp.run([x, y])
    return comp.output_list[-1]


if __name__ == '__main__':
    the_input = list(map(int, read_input("day19", ",")))

    m = Matrix().init_from_elem(50, 20, ".")
    for x, y in m.index_range():
        is_pulled = run_program(the_input, x, y)
        if is_pulled == 1:
            m.set(x, y, "#")

    m.print_top_left(print_headers=True)

    beam_area = len(list(filter(lambda i: i == "#", m.values)))

    print()
    print("Part One: ", beam_area)

# --- Part Two ---
#
# You aren't sure how large Santa's ship is. You aren't even sure if you'll need to use this thing on Santa's ship, but it doesn't hurt to be prepared. You figure Santa's ship might fit in a 100x100 square.