Skip to content

Instantly share code, notes, and snippets.

@spielkind
Created December 14, 2021 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spielkind/1bf030b175a8b698453f6385e2738b26 to your computer and use it in GitHub Desktop.
Save spielkind/1bf030b175a8b698453f6385e2738b26 to your computer and use it in GitHub Desktop.
#!/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