Example #1
0
def step(g):
    global flashes

    flash = intgrid.Grid(g.width, g.height)

    for x in range(g.width):
        for y in range(g.height):
            g.set(x, y, g.get(x, y) + 1)

    progress = True
    while progress:
        progress = False
        ng = intgrid.Grid()
        ng.copy(g)
        for x in range(ng.width):
            for y in range(ng.height):
                if ng.get(x, y) > 9 and not flash.get(x, y):
                    flashes += 1
                    flash.set(x, y, 1)
                    n = ng.neighbors(x, y)
                    for nx, ny in n:
                        ng.set(nx, ny, ng.get(nx, ny) + 1)
                    progress = True
        g = ng

    for x in range(g.width):
        for y in range(g.height):
            if g.get(x, y) > 9:
                g.set(x, y, 0)

    return g
Example #2
0
def make_detailed_grid(g):
    h = intgrid.Grid(g.width * 5, g.height * 5)
    for i in range(5):
        for j in range(5):
            for x in range(g.width):
                for y in range(g.height):
                    h.set(x + i * g.width, y + j * g.height,
                          wrap(g.get(x, y) + i + j))

    return h
Example #3
0
# 9 wraps to 1 ... there's surely a numerical way to do this
def wrap(n):
    if n > 9:
        n -= 9
    return n


def make_detailed_grid(g):
    h = intgrid.Grid(g.width * 5, g.height * 5)
    for i in range(5):
        for j in range(5):
            for x in range(g.width):
                for y in range(g.height):
                    h.set(x + i * g.width, y + j * g.height,
                          wrap(g.get(x, y) + i + j))

    return h


if __name__ == "__main__":
    with open(sys.argv[1], "r") as f:
        g = intgrid.Grid()
        g.read(f)

    g = make_detailed_grid(g)     # part 2 only

    p = astar(g, (0, 0), (g.width - 1, g.height - 1))

    print(p)