Advent of Code 2021 Python Solution: Day 11

qviper

Viper

Posted on December 11, 2021

Advent of Code 2021 Python Solution: Day 11

Today's challenge was harder than previous day's.

adj = [(i,j) for i in range(-1, 2) for j in range(-1,2) if i!=0 or j!=0]
window = {(i,j):darr[i][j] for i in range(10) for j in range(10)}

flashes = 0
i=0
previous = set()

while len(previous)<len(window):
    previous = set()
    window = {k:v+1 for k, v in window.items()}
    while True:
        if sum(v>9 for k,v in window.items() if k not in previous)==0:
            break

        for k,v in window.items():
            if k not in previous and v>9:
                previous.add(k)
                for ad in [(k[0]+i,k[1]+j) for i,j in adj if (k[0]+i,k[1]+j) in window]:
                    window[ad]+=1

    flashes+=len(previous)
    window.update({k:0 for k in previous})
    i+=1
    if i==100:
        print(f"Part 1: {flashes}")

print(f"Part 2: {i}")
Enter fullscreen mode Exit fullscreen mode

Why not read more?

💖 💪 🙅 🚩
qviper
Viper

Posted on December 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related