Untitled

 avatar
unknown
python
3 years ago
743 B
5
Indexable
def simulate_sand(rocks:set, lowest:int, floor:int|None):
    # simulate sand
    origin = 500
    # counter for rest unit of sand
    rest, sand = 0, origin
    while True:
        # check if steps are possible (in order)
        for step in [0 + 1j, -1 +1j, 1 + 1j]:
            tmp_sand = sand + step
            if tmp_sand in rocks or (floor and tmp_sand.imag == floor):
                continue
            sand = tmp_sand
            break
        # otherwise rest and create new sand
        else:
            rest += 1
            rocks.add(sand)
            sand = origin

        # check if over
        if origin in rocks or (floor is None and sand.imag > lowest[sand.real]):
            break
    return rest
Editor is loading...