Exemplo n.º 1
0
def transform(area):
    open_ = area == OPEN
    trees = area == TREES
    yards = area == LUMBERYARD

    adj_trees = convolve_2d(trees, ADJACENT)
    adj_yards = convolve_2d(yards, ADJACENT)

    area = np.where(open_ & (adj_trees >= 3), TREES, area)
    area = np.where(trees & (adj_yards >= 3), LUMBERYARD, area)
    area = np.where(yards & (adj_yards * adj_trees == 0), OPEN, area)
    return area
Exemplo n.º 2
0
def find_portals(maze):
    walls = np.where(maze == ".", " ", maze) != " "
    pattern = np.array([[-1, 1, -1], [-1, 1, -1], [1, -1, 1]])

    portals = []
    for i, v in enumerate((RIGHT, UP, LEFT, DOWN)):
        coords = np.argwhere(convolve_2d(walls, np.rot90(pattern, i)) == 4)
        for x, y in coords:
            label = "".join(maze[x - 1:x + 2, y - 1:y + 2].reshape(9))
            label = re.sub(r"[\s#.]", "", label)
            pos = Vect2D(x, y) + v
            portals.append((pos, label))

    return portals
Exemplo n.º 3
0
def recursive_step(bugs, inner=None, outer=None):
    neighbors = convolve_2d(bugs, FILTER)

    if inner is not None:
        neighbors[1, 2] += np.sum(inner[0, :])
        neighbors[2, 1] += np.sum(inner[:, 0])
        neighbors[3, 2] += np.sum(inner[4, :])
        neighbors[2, 3] += np.sum(inner[:, 4])

    if outer is not None:
        neighbors[0, :] += outer[1, 2]
        neighbors[:, 0] += outer[2, 1]
        neighbors[4, :] += outer[3, 2]
        neighbors[:, 4] += outer[2, 3]

    one_adj, two_adj = neighbors == 1, neighbors == 2
    bugs_alive = bugs & one_adj
    infected = (~bugs) & (one_adj | two_adj)
    bugs = bugs_alive | infected
    bugs[2, 2] = 0
    return bugs * 1
Exemplo n.º 4
0
def total_power(power_levels, square=3):
    power_sums = convolve_2d(power_levels, np.ones((square, square), dtype=int), 0)
    x, y = np.unravel_index(power_sums.argmax(), power_sums.shape)
    return x + 1, y + 1, power_sums[x, y]
Exemplo n.º 5
0
def step(bugs):
    neighbors = convolve_2d(bugs, FILTER)
    one_adj, two_adj = neighbors == 1, neighbors == 2
    bugs_alive = bugs & one_adj
    infected = (~bugs) & (one_adj | two_adj)
    return bugs_alive | infected