--- Day 17: Pyroclastic Flow ---

 avatar
unknown
python
3 years ago
2.3 kB
92
Indexable
import copy

data = []
with open("testInput", "r") as file:
    for line in file:
        data.append(line.strip())

def moveLeft(shape: list):
    for point in shape:
        point[1] -= 1
def moveRight(shape: list):
    for point in shape:
        point[1] += 1
def moveDown(shape: list):
    for point in shape:
        point[0] -= 1
def moveUp(shape: list):
    for point in shape:
        point[0] += 1

def adjustPosition(shape: list, topPosition: int, k: int):
    for point in shape:
        point[0] += (topPosition + k)
    return shape

def checkCollision(shape: list, fullPicture):
    for point in shape:
        point = (point[0],point[1])
        if point in fullPicture:
            return True
        if point[1]<0 or point[1]>6:
            return True
        if point[0]<0:
            return True
    return False

shapes = [
    [[0,2],[0,3],[0,4],[0,5]],
    [[0,3],[1,2],[1,3],[1,4],[2,3]],
    [[0,2],[0,3],[0,4],[1,4],[2,4]],
    [[0,2], [1,2], [2,2], [3,2]],
    [[0,2], [1,2], [0,3], [1,3]]
]

fullPicture = set()
currentShape = 0
top = 0
fallingShape = list(shapes[currentShape])
adjustPosition(fallingShape, top, 3)
stoppedRocks = 0

windPattern = data[0]

currentWind = 0
debug = False

while True:
    dir = windPattern[currentWind]

    currentWind = (currentWind + 1) % len(windPattern)
    match dir:
        case '>':
            moveRight(fallingShape)
            if checkCollision(fallingShape, fullPicture):
                moveLeft(fallingShape)

        case '<':
            moveLeft(fallingShape)
            if checkCollision(fallingShape, fullPicture):
                moveRight(fallingShape)

    moveDown(fallingShape)
    if checkCollision(fallingShape, fullPicture):
        moveUp(fallingShape)
        stoppedRocks += 1
        top = max( max([x[0] for x in fallingShape]), top )

        if stoppedRocks == 2022:
            print(top+1) # answer
            break
        
        for point in fallingShape:
            fullPicture.add((point[0], point[1]))

        currentShape = (currentShape + 1) % 5
        fallingShape = copy.deepcopy(shapes[currentShape])
        adjustPosition(fallingShape, top, 4)

Editor is loading...