Info
The numbers 1 to 9 each represent a cell in the Moore's Neighbourhood, with 5 being the central cell. So:
123
456
789
1={-1,-1} 2={-1, 0} 3={-1, 1}
4={ 0,-1} 5={ 0, 0} 6={ 0, 1}
7={ 1,-1} 8={ 1, 0} 9={ 1, 1}
The Challenge
You may take input via STDIN, ARGV or function argument and either return the result or print it to STDOUT. Input is a N x N grid (torus topology, meaning if x or y is < 1 then x or y = N, and if x or y > N then x or y = 1), and your program must output one interation of that grid by replacing each cell with the value at its Moore's Neighbourhood cell.
Example Input grid (2 x 2):
13
79
Output:
97
31
Explanation:
Starting at position 1,1 we have the value 1, since value 1={-1,-1} we have to retrieve 1+(-1),1+(-1) = 0,0. And because it's a torus 0,0 we wrap around to N. So we retrieve the cell value at position 1,1 (1) with the cell value at position 2,2 (9).
For the next cell 1,2 we have value 3 (=-1, 1) so 1+(-1),2+(1) = 0,3. Wraps around to 2,1 which is value 7.
Next cell value at 2,1 is 7 (= 1,-1) so 2+(1),1+(-1) = 3,0. Wraps around to 1,2 which is value 3.
Next cell value at 2,2 is 9 (= 1, 1) so 2+(1),2+(1) = 3,3. Wraps around to 1,1 which is value 1.
More Examples
Input Grid (3 x 3):
123
456
789
Expected Output:
987
654
321
Input Grid (5 x 5):
77497
81982
32236
96336
67811
Expected Output:
28728
37337
11923
73369
77433
Final Notes
If you have any question, don't hesitate to comment. This is a code golf challenge, shortest code wins!