Day 11
Ah - another grid one! I think I'll start posting my 'raw code' more often now as the puzzles get longer and I have less time to refactor. Thankfully, I managed to come first in the school for both parts - decreasing my point defecit to the anonymous user (who I reckon overslept).
For Part 1, I read this and noticed that Eric finally checked off the box of including Conway's Game of Life in 2021! It's a very well known simulation by John Conway - great to learn and practice with. I in fact had code to solve this type of puzzle ready to use, but the puzzle was modified enough for that not to be possible :( However, this being my strength, I managed to throw together a fast implementation. I was really surprised to see a recursive solution working! It's just what I thought of at 5am. I suspect that updating the entire grid at once by creating copies of it would have been better, but my instinct was to just use a set to keep track of which octopi had already been flashed and not flash them again - this allowed for a speedy part 2!
For the Part 2 code, replace the lines with comments with the code after it. All I did here was to check if the length of the set of flashed octopi was equal to the number of octopi and then I had my answer! A simple off-by-one error occurred here and slowed me down though. I saw a fast implementation of this where you just use a flag and assume all octopi have flashed... until one hasn't, but as I already had a set I thought I could use that. I used sys.exit to terminate the program once I had an answer, like in Day 4. Almost as fun as day 8 :)
The xmas holidays have begun...!!!!