Back
Close
  • 297

Statement

 Goal

A sandpile is a square matrix of natural numbers between 0 and 3, representing how many grains of sand there is on each square
To add two sandpiles, just start by adding the two matrices element by element. Except the matrix you generate might not be a sandpile, if one of its element is higher than 3 you must transform this matrix into a sandpile, and this is how it is done :
- If a square has 4 grains of sand or more, it "loses" four and distributes it to its four neighbors (if the square touches an edge, the grain of sand is lost)
- Keep doing that to all the squares with 4 grains or more until all the squares have 3 grains or less

Example :
000   000   000    010
020 + 020 = 040 -> 101
000 000 000 010
Input
Line 1 : An integer n, the size of the two sandpiles
2*n next lines : The two sandpiles
Output
n lines representing the resulting sandpile
Constraints
2 ≤ N ≤ 10
Example
Input
3
121
202
121
020
202
020
Output
313
101
313
Solve it

A higher resolution is required to access the IDE

codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants