My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2021/11

+52
+52
2021/day11.py
··· 1 + import copy 2 + import fileinput 3 + from utils import Point 4 + 5 + board = {} 6 + for y, line in enumerate(fileinput.input()): 7 + for x, c in enumerate(line.strip()): 8 + board[Point(x, y)] = int(c) 9 + 10 + 11 + part_1 = 0 12 + 13 + for i in range(10000000): 14 + next_state = copy.copy(board) 15 + 16 + for pos, val in next_state.items(): 17 + next_state[pos] = val + 1 18 + 19 + flashed = set() 20 + while True: # when steady state 21 + updated = False 22 + 23 + for pos, val in next_state.items(): 24 + # Flash! 25 + if next_state[pos] > 9 and pos not in flashed: 26 + # Increment all 8 neighbours 27 + for n in pos.neighbours_8(): 28 + if n not in next_state: 29 + continue 30 + 31 + next_state[n] = next_state[n] + 1 32 + 33 + flashed.add(pos) 34 + part_1 += 1 35 + updated = True 36 + 37 + if not updated: 38 + break 39 + 40 + if len(flashed) == len(next_state): 41 + print "Part 2:", i + 1 42 + break 43 + 44 + # Reset all >9s to 0 45 + for pos, val in next_state.items(): 46 + if val > 9: 47 + next_state[pos] = 0 48 + 49 + board = next_state 50 + 51 + if i == 99: 52 + print "Part 1:", part_1