Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details.
As we all know, Santa comes down the chimney to deliver presents to good kids. But chimneys are normally full of smoke, and the Elves are worried about Santa's health. So they decide to blow the smoke out using an electronic air blower.
A chimney is modeled as a rectangular grid. The number in each cell represents the amount of smoke in the region. The left and right sides are walls, the top is open, and the air blower is blowing wind from the bottom.
||========||
|| |99| ||
||--+--+--||
|| | |24||
||--+--+--||
||36| |12||
||--+--+--||
|| | | ||
||--+--+--||
|| |50| ||
||^^^^^^^^||
Every second, the following happens in order. At any step, the smoke that goes outside of the chimney through the top is excluded from simulation.
- The smoke is blown up one cell upwards.
- The smoke diffuses to adjacent (4-way) cells (including outside).
- Let's say a cell contains
n
amount of smoke. Then, for each available direction, exactlyfloor(n/5)
amount of smoke moves in that direction.
- Let's say a cell contains
Given the initial state above, smoke will move and diffuse as follows:
After step 1:
||========||
|| | |24||
||--+--+--||
||36| |12||
||--+--+--||
|| | | ||
||--+--+--||
|| |50| ||
||--+--+--||
|| | | ||
||^^^^^^^^||
After step 2: (1 second passed)
||========||
||7 |4 |14||
||--+--+--||
||15|9 |10||
||--+--+--||
||7 |10|2 ||
||--+--+--||
||10|10|10||
||--+--+--||
|| |10| ||
||^^^^^^^^||
2 seconds passed:
||========||
||8 |12|5 ||
||--+--+--||
||11|6 |8 ||
||--+--+--||
||7 |10|6 ||
||--+--+--||
||4 |4 |4 ||
||--+--+--||
|| |2 | ||
||^^^^^^^^||
5 seconds passed:
||========||
||5 |1 |5 ||
||--+--+--||
||1 |4 |1 ||
||--+--+--||
|| | | ||
||--+--+--||
|| | | ||
||--+--+--||
|| | | ||
||^^^^^^^^||
This chimney is completely clear of smoke after 7 seconds.
Task
Given a 2D grid of non-negative integers which represents the current state of smoke in the chimney, simulate the chimney as specified above and output the state 1 second later. You may assume that the chimney is at least 2 units wide and 2 units tall.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
[[0, 99, 0], [0, 0, 24], [36, 0, 12], [0, 0, 0], [0, 50, 0]]
-> [[7, 4, 14], [15, 9, 10], [7, 10, 2], [10, 10, 10], [0, 10, 0]]
-> [[8, 12, 5], [11, 6, 8], [7, 10, 6], [4, 4, 4], [0, 2, 0]]
-> [[7, 7, 7], [8, 5, 6], [5, 6, 5], [0, 2, 0], [0, 0, 0]]
-> [[7, 4, 5], [4, 5, 4], [1, 3, 1], [0, 0, 0], [0, 0, 0]]
-> [[5, 1, 5], [1, 4, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
-> [[1, 4, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
-> [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]