Octopuses flash when their energy levels reach above 9 and it causes their neighbors to increase their levels by 1 too? This sounds like a perfect job for some kind of a matrix operation…
Loading data:
In this challenge I decided to try my hand at using convolution matrix to solve this problem. First I had to create a function that would return an array containing octopuses and their neighbors. For this I used the following code:
The function as_strided is a pretty esoteric function, which operates directly on memory, which also means a lot can go very wrong when using it.
Here I used this function to calculate how the energy levels would progress through the time:
I used a convolution matrix of the following shape:
Task 1
Here we sum all the flashes that happened in all of the iterations with np.sum:
Task 2
Here we check when all the octopuses flashed with np.all: