Created
December 14, 2021 16:35
-
-
Save spielkind/1bf030b175a8b698453f6385e2738b26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
with open('day11.txt') as f: | |
octopuses = [list(map(int, list(line.strip()))) for line in f] | |
def get_neighbours(p): | |
neighbours = [] | |
(x, y) = p[0], p[1] | |
if x != 0: | |
neighbours.append((x-1, y)) | |
if y != 0: neighbours.append((x-1, y-1)) | |
if y != 9: neighbours.append((x-1, y+1)) | |
if x != 9: | |
neighbours.append((x+1, y)) | |
if y != 0: neighbours.append((x+1, y-1)) | |
if y != 9: neighbours.append((x+1, y+1)) | |
if y != 0: neighbours.append((x, y-1)) | |
if y != 9: neighbours.append((x, y+1)) | |
return neighbours | |
def flash(p): | |
(x, y) = p[0], p[1] | |
octopuses[y][x] = 0 | |
flashed.append(p) | |
flashes = 1 | |
for n in get_neighbours(p): | |
if n not in flashed: octopuses[n[1]][n[0]] += 1 | |
if octopuses[n[1]][n[0]] > 9: | |
flashes += flash(n) | |
return flashes | |
flashes = 0 | |
steps = 0 | |
while True: | |
flashed = [] | |
for x in range(10): | |
for y in range(10): | |
if (x, y) not in flashed: octopuses[y][x] += 1 | |
if octopuses[y][x] > 9: | |
flashes += flash((x, y)) | |
steps += 1 | |
if steps == 100: print('Part One', flashes) | |
if len(flashed) == 100: break | |
print('Part Two:', steps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment