9
\$\begingroup\$

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!

asked Sep 19, 2014 at 20:51
\$\endgroup\$
4
  • \$\begingroup\$ How do you define the position of an element? I don't understand how the input set of cells are indexed. \$\endgroup\$ Commented Sep 19, 2014 at 21:10
  • \$\begingroup\$ @Rainbolt The input is indexed by rows then columns. So in the last example (input) cell value at 2,3 is 9. row 2 = 81982, and the third column of that is 9. \$\endgroup\$ Commented Sep 19, 2014 at 21:12
  • 1
    \$\begingroup\$ related: codegolf.stackexchange.com/q/36839/15599 \$\endgroup\$ Commented Sep 19, 2014 at 21:35
  • 4
    \$\begingroup\$ Could not help but read "Moore examples". \$\endgroup\$ Commented Sep 20, 2014 at 21:11

4 Answers 4

9
\$\begingroup\$

APL (33)

APL was made for this. This is a function that takes the input grid as an N-by-N matrix and returns the output grid as an N-by-N matrix.

{(⍳⍴⍵)⌷ ̈(,∆∘.⊖(∆←2-⌽⍳3)∘.⌽⊂⍵)[⍵]}

Test:

 ⍝ define input matrices
 ∆1 ← ↑(1 3)(7 9)
 ∆2 ← ↑(1 2 3)(4 5 6)(7 8 9)
 ∆3 ← ↑(7 7 4 9 7)(8 1 9 8 2)(3 2 2 3 6)(9 6 3 3 6)(6 7 8 1 1)
 ⍝ show input matrices
 ∆1 ∆2 ∆3
 1 3 1 2 3 7 7 4 9 7 
 7 9 4 5 6 8 1 9 8 2 
 7 8 9 3 2 2 3 6 
 9 6 3 3 6 
 6 7 8 1 1 
 ⍝ apply function to input matrices giving output matrices
 {(⍳⍴⍵)⌷ ̈(,∆∘.⊖(∆←2-⌽⍳3)∘.⌽⊂⍵)[⍵]} ̈ ∆1 ∆2 ∆3
 9 7 9 8 7 2 8 7 2 8 
 3 1 6 5 4 3 7 3 3 7 
 3 2 1 1 1 9 2 3 
 7 3 3 6 9 
 7 7 4 3 3 
answered Sep 19, 2014 at 22:15
\$\endgroup\$
5
\$\begingroup\$

Python, 174

def t(b):b=b.split("\n");E=enumerate;C=[-1]*3+[0]*3+[1]*3+[-1,0,1]*3;print"\n".join("".join(b[(i+C[int(x)-1])%len(b)][(j+C[int(x)+8])%len(y)]for j,x in E(y))for i,y in E(b))

Python was not made for this... APL was!

answered Sep 19, 2014 at 22:40
\$\endgroup\$
3
\$\begingroup\$

Python, 105

Takes and returns a list of lists:

def f(a):e=enumerate;n=len(a);return[[a[(y+(v-1)/3-1)%n][(x+(v-1)%3-1)%n]for x,v in e(u)]for y,u in e(a)]

Takes and returns a string (148 characters):

def f(s):
 a=[map(int,l)for l in s.split()];n=len(a);e=enumerate
 for y,u in e(a):print''.join(`a[(y+(v-1)/3-1)%n][(x+(v-1)%3-1)%n]`for x,v in e(u))
answered Sep 20, 2014 at 15:25
\$\endgroup\$
2
\$\begingroup\$

MATLAB - 121 bytes

function O=X(I),m=size(I,1);n=3*m;o=-1:1;c=o<2;M=c'*o+n*o'*c;D=repmat(I,3);C=D;C(:)=1:n*n;t=m+(1:m);O=C+M(D);O=D(O(t,t));

MATLAB was slightly less made for this than APL, but slightly more made for this than Python. ;)

Test Output

X( [1 2 3; 4 5 6; 7 8 9] )
ans =
 9 8 7
 6 5 4
 3 2 1
X( [7 7 4 9 7; 8 1 9 8 2; 3 2 2 3 6; 9 6 3 3 6; 6 7 8 1 1] )
ans =
 2 8 7 2 8
 3 7 3 3 7
 1 1 9 2 3
 7 3 3 6 9
 7 7 4 3 3
answered Sep 19, 2014 at 23:15
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.