Whose neighbours are hostile?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
10
down vote

favorite












Introduction



For the purposes of this challenge, we will define the neighbours of an element $E$ in a square matrix $A$ (such that $E=A_i,j$) as all the entries of $A$ that are immediately adjacent diagonally, horizontally or vertically to $E$ (i.e. they "surround" $E$, without wrapping around).



For pedants, a formal definition of the neighbours of $A_i,:j$ for an $ntimes n$ matix $A$ is (0-indexed):
$$N_i,:j=A_a,:bmid(a,b)in E_i,:j:cap:([0,:n):cap:BbbZ)^2$$
where
$$E_i,:j=i-1,:i,:i+1times j-1,:j,:j+1 text \ i,:j$$



Let's say that the element at index $i,:j$ lives in hostility if it is coprime to all its neighbours (that is, $gcd(A_i,:j,:n)=1:forall:nin N_i,:j$). Sadly, this poor entry can't borrow even a cup of sugar from its rude nearby residents...



Task



Enough stories: Given a square matrix $M$ of positive integers, output one of the following:



  • A flat list of elements (deduplicated or not) indicating all entries that occupy some indices $i,j$ in $M$ such that the neighbours $N_i,:j$ are hostile.

  • A boolean matrix with $1$s at positions where the neighbours are hostile and $0$ otherwise (you can choose any other consistent values in place of $0$ and $1$).

  • The list of pairs of indices $i,:j$ that represent hostile neighbourhoods.

Reference Implementation in Physica – supports Python syntax as well for I/O. You can take input and provide output through any standard method and in any reasonable format, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest code in bytes (in every language) wins!



Moreover, you can take the matrix size as input too and additionally can take the matrix as a flat list since it will always be square.



Example



Consider the following matrix:



$$left(beginmatrix
64 & 10 & 14 \
27 & 22 & 32 \
53 & 58 & 36 \
endmatrixright)$$



The corresponding neighbours of each element are:



i j – E -> Neighbours | All coprime to E?
|
0 0 – 64 -> 10; 27; 22 | False
0 1 – 10 -> 64; 14; 27; 22; 32 | False
0 2 – 14 -> 10; 22; 32 | False
1 0 – 27 -> 64; 10; 22; 53; 58 | True
1 1 – 22 -> 64; 10; 14; 27; 32; 53; 58; 36 | False
1 2 – 32 -> 10; 14; 22; 58; 36 | False
2 0 – 53 -> 27; 22; 58 | True
2 1 – 58 -> 27; 22; 32; 53; 36 | False
2 2 – 36 -> 22; 32; 58 | False


And thus the output must be one of the following:



  • 27; 53

  • 0; 0; 0; 1; 0; 0; 1; 0; 0

  • (1; 0); (2; 0)

Test cases



Input –> Version 1 | Version 2 | Version 3

[[36, 94], [24, 69]] ->

[[0, 0], [0, 0]]

[[38, 77, 11], [17, 51, 32], [66, 78, 19]] –>
[38, 19]
[[1, 0, 0], [0, 0, 0], [0, 0, 1]]
[(0, 0), (2, 2)]
[[64, 10, 14], [27, 22, 32], [53, 58, 36]] ->
[27, 53]
[[0, 0, 0], [1, 0, 0], [1, 0, 0]]
[(1, 0), (2, 0)]
[[9, 9, 9], [9, 3, 9], [9, 9, 9]] ->

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

[[1, 1, 1], [1, 1, 1], [1, 1, 1]] ->
[1, 1, 1, 1, 1, 1, 1, 1, 1] or [1]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
[[35, 85, 30, 71], [10, 54, 55, 73], [80, 78, 47, 2], [33, 68, 62, 29]] ->
[71, 73, 47, 29]
[[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
[(0, 3), (1, 3), (2, 2), (3, 3)]









share|improve this question























  • Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
    – Arnauld
    Sep 16 at 13:17










  • Can we take the matrix size as input?
    – Delfad0r
    Sep 17 at 5:32










  • @Delfad0r I always forget to mention that. Yes, you may take the matrix size as input.
    – Mr. Xcoder
    Sep 17 at 7:19














up vote
10
down vote

favorite












Introduction



For the purposes of this challenge, we will define the neighbours of an element $E$ in a square matrix $A$ (such that $E=A_i,j$) as all the entries of $A$ that are immediately adjacent diagonally, horizontally or vertically to $E$ (i.e. they "surround" $E$, without wrapping around).



For pedants, a formal definition of the neighbours of $A_i,:j$ for an $ntimes n$ matix $A$ is (0-indexed):
$$N_i,:j=A_a,:bmid(a,b)in E_i,:j:cap:([0,:n):cap:BbbZ)^2$$
where
$$E_i,:j=i-1,:i,:i+1times j-1,:j,:j+1 text \ i,:j$$



Let's say that the element at index $i,:j$ lives in hostility if it is coprime to all its neighbours (that is, $gcd(A_i,:j,:n)=1:forall:nin N_i,:j$). Sadly, this poor entry can't borrow even a cup of sugar from its rude nearby residents...



Task



Enough stories: Given a square matrix $M$ of positive integers, output one of the following:



  • A flat list of elements (deduplicated or not) indicating all entries that occupy some indices $i,j$ in $M$ such that the neighbours $N_i,:j$ are hostile.

  • A boolean matrix with $1$s at positions where the neighbours are hostile and $0$ otherwise (you can choose any other consistent values in place of $0$ and $1$).

  • The list of pairs of indices $i,:j$ that represent hostile neighbourhoods.

Reference Implementation in Physica – supports Python syntax as well for I/O. You can take input and provide output through any standard method and in any reasonable format, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest code in bytes (in every language) wins!



Moreover, you can take the matrix size as input too and additionally can take the matrix as a flat list since it will always be square.



Example



Consider the following matrix:



$$left(beginmatrix
64 & 10 & 14 \
27 & 22 & 32 \
53 & 58 & 36 \
endmatrixright)$$



The corresponding neighbours of each element are:



i j – E -> Neighbours | All coprime to E?
|
0 0 – 64 -> 10; 27; 22 | False
0 1 – 10 -> 64; 14; 27; 22; 32 | False
0 2 – 14 -> 10; 22; 32 | False
1 0 – 27 -> 64; 10; 22; 53; 58 | True
1 1 – 22 -> 64; 10; 14; 27; 32; 53; 58; 36 | False
1 2 – 32 -> 10; 14; 22; 58; 36 | False
2 0 – 53 -> 27; 22; 58 | True
2 1 – 58 -> 27; 22; 32; 53; 36 | False
2 2 – 36 -> 22; 32; 58 | False


And thus the output must be one of the following:



  • 27; 53

  • 0; 0; 0; 1; 0; 0; 1; 0; 0

  • (1; 0); (2; 0)

Test cases



Input –> Version 1 | Version 2 | Version 3

[[36, 94], [24, 69]] ->

[[0, 0], [0, 0]]

[[38, 77, 11], [17, 51, 32], [66, 78, 19]] –>
[38, 19]
[[1, 0, 0], [0, 0, 0], [0, 0, 1]]
[(0, 0), (2, 2)]
[[64, 10, 14], [27, 22, 32], [53, 58, 36]] ->
[27, 53]
[[0, 0, 0], [1, 0, 0], [1, 0, 0]]
[(1, 0), (2, 0)]
[[9, 9, 9], [9, 3, 9], [9, 9, 9]] ->

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

[[1, 1, 1], [1, 1, 1], [1, 1, 1]] ->
[1, 1, 1, 1, 1, 1, 1, 1, 1] or [1]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
[[35, 85, 30, 71], [10, 54, 55, 73], [80, 78, 47, 2], [33, 68, 62, 29]] ->
[71, 73, 47, 29]
[[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
[(0, 3), (1, 3), (2, 2), (3, 3)]









share|improve this question























  • Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
    – Arnauld
    Sep 16 at 13:17










  • Can we take the matrix size as input?
    – Delfad0r
    Sep 17 at 5:32










  • @Delfad0r I always forget to mention that. Yes, you may take the matrix size as input.
    – Mr. Xcoder
    Sep 17 at 7:19












up vote
10
down vote

favorite









up vote
10
down vote

favorite











Introduction



For the purposes of this challenge, we will define the neighbours of an element $E$ in a square matrix $A$ (such that $E=A_i,j$) as all the entries of $A$ that are immediately adjacent diagonally, horizontally or vertically to $E$ (i.e. they "surround" $E$, without wrapping around).



For pedants, a formal definition of the neighbours of $A_i,:j$ for an $ntimes n$ matix $A$ is (0-indexed):
$$N_i,:j=A_a,:bmid(a,b)in E_i,:j:cap:([0,:n):cap:BbbZ)^2$$
where
$$E_i,:j=i-1,:i,:i+1times j-1,:j,:j+1 text \ i,:j$$



Let's say that the element at index $i,:j$ lives in hostility if it is coprime to all its neighbours (that is, $gcd(A_i,:j,:n)=1:forall:nin N_i,:j$). Sadly, this poor entry can't borrow even a cup of sugar from its rude nearby residents...



Task



Enough stories: Given a square matrix $M$ of positive integers, output one of the following:



  • A flat list of elements (deduplicated or not) indicating all entries that occupy some indices $i,j$ in $M$ such that the neighbours $N_i,:j$ are hostile.

  • A boolean matrix with $1$s at positions where the neighbours are hostile and $0$ otherwise (you can choose any other consistent values in place of $0$ and $1$).

  • The list of pairs of indices $i,:j$ that represent hostile neighbourhoods.

Reference Implementation in Physica – supports Python syntax as well for I/O. You can take input and provide output through any standard method and in any reasonable format, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest code in bytes (in every language) wins!



Moreover, you can take the matrix size as input too and additionally can take the matrix as a flat list since it will always be square.



Example



Consider the following matrix:



$$left(beginmatrix
64 & 10 & 14 \
27 & 22 & 32 \
53 & 58 & 36 \
endmatrixright)$$



The corresponding neighbours of each element are:



i j – E -> Neighbours | All coprime to E?
|
0 0 – 64 -> 10; 27; 22 | False
0 1 – 10 -> 64; 14; 27; 22; 32 | False
0 2 – 14 -> 10; 22; 32 | False
1 0 – 27 -> 64; 10; 22; 53; 58 | True
1 1 – 22 -> 64; 10; 14; 27; 32; 53; 58; 36 | False
1 2 – 32 -> 10; 14; 22; 58; 36 | False
2 0 – 53 -> 27; 22; 58 | True
2 1 – 58 -> 27; 22; 32; 53; 36 | False
2 2 – 36 -> 22; 32; 58 | False


And thus the output must be one of the following:



  • 27; 53

  • 0; 0; 0; 1; 0; 0; 1; 0; 0

  • (1; 0); (2; 0)

Test cases



Input –> Version 1 | Version 2 | Version 3

[[36, 94], [24, 69]] ->

[[0, 0], [0, 0]]

[[38, 77, 11], [17, 51, 32], [66, 78, 19]] –>
[38, 19]
[[1, 0, 0], [0, 0, 0], [0, 0, 1]]
[(0, 0), (2, 2)]
[[64, 10, 14], [27, 22, 32], [53, 58, 36]] ->
[27, 53]
[[0, 0, 0], [1, 0, 0], [1, 0, 0]]
[(1, 0), (2, 0)]
[[9, 9, 9], [9, 3, 9], [9, 9, 9]] ->

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

[[1, 1, 1], [1, 1, 1], [1, 1, 1]] ->
[1, 1, 1, 1, 1, 1, 1, 1, 1] or [1]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
[[35, 85, 30, 71], [10, 54, 55, 73], [80, 78, 47, 2], [33, 68, 62, 29]] ->
[71, 73, 47, 29]
[[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
[(0, 3), (1, 3), (2, 2), (3, 3)]









share|improve this question















Introduction



For the purposes of this challenge, we will define the neighbours of an element $E$ in a square matrix $A$ (such that $E=A_i,j$) as all the entries of $A$ that are immediately adjacent diagonally, horizontally or vertically to $E$ (i.e. they "surround" $E$, without wrapping around).



For pedants, a formal definition of the neighbours of $A_i,:j$ for an $ntimes n$ matix $A$ is (0-indexed):
$$N_i,:j=A_a,:bmid(a,b)in E_i,:j:cap:([0,:n):cap:BbbZ)^2$$
where
$$E_i,:j=i-1,:i,:i+1times j-1,:j,:j+1 text \ i,:j$$



Let's say that the element at index $i,:j$ lives in hostility if it is coprime to all its neighbours (that is, $gcd(A_i,:j,:n)=1:forall:nin N_i,:j$). Sadly, this poor entry can't borrow even a cup of sugar from its rude nearby residents...



Task



Enough stories: Given a square matrix $M$ of positive integers, output one of the following:



  • A flat list of elements (deduplicated or not) indicating all entries that occupy some indices $i,j$ in $M$ such that the neighbours $N_i,:j$ are hostile.

  • A boolean matrix with $1$s at positions where the neighbours are hostile and $0$ otherwise (you can choose any other consistent values in place of $0$ and $1$).

  • The list of pairs of indices $i,:j$ that represent hostile neighbourhoods.

Reference Implementation in Physica – supports Python syntax as well for I/O. You can take input and provide output through any standard method and in any reasonable format, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest code in bytes (in every language) wins!



Moreover, you can take the matrix size as input too and additionally can take the matrix as a flat list since it will always be square.



Example



Consider the following matrix:



$$left(beginmatrix
64 & 10 & 14 \
27 & 22 & 32 \
53 & 58 & 36 \
endmatrixright)$$



The corresponding neighbours of each element are:



i j – E -> Neighbours | All coprime to E?
|
0 0 – 64 -> 10; 27; 22 | False
0 1 – 10 -> 64; 14; 27; 22; 32 | False
0 2 – 14 -> 10; 22; 32 | False
1 0 – 27 -> 64; 10; 22; 53; 58 | True
1 1 – 22 -> 64; 10; 14; 27; 32; 53; 58; 36 | False
1 2 – 32 -> 10; 14; 22; 58; 36 | False
2 0 – 53 -> 27; 22; 58 | True
2 1 – 58 -> 27; 22; 32; 53; 36 | False
2 2 – 36 -> 22; 32; 58 | False


And thus the output must be one of the following:



  • 27; 53

  • 0; 0; 0; 1; 0; 0; 1; 0; 0

  • (1; 0); (2; 0)

Test cases



Input –> Version 1 | Version 2 | Version 3

[[36, 94], [24, 69]] ->

[[0, 0], [0, 0]]

[[38, 77, 11], [17, 51, 32], [66, 78, 19]] –>
[38, 19]
[[1, 0, 0], [0, 0, 0], [0, 0, 1]]
[(0, 0), (2, 2)]
[[64, 10, 14], [27, 22, 32], [53, 58, 36]] ->
[27, 53]
[[0, 0, 0], [1, 0, 0], [1, 0, 0]]
[(1, 0), (2, 0)]
[[9, 9, 9], [9, 3, 9], [9, 9, 9]] ->

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

[[1, 1, 1], [1, 1, 1], [1, 1, 1]] ->
[1, 1, 1, 1, 1, 1, 1, 1, 1] or [1]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
[[35, 85, 30, 71], [10, 54, 55, 73], [80, 78, 47, 2], [33, 68, 62, 29]] ->
[71, 73, 47, 29]
[[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
[(0, 3), (1, 3), (2, 2), (3, 3)]






code-golf array-manipulation arithmetic integer matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 17 at 7:20

























asked Sep 16 at 12:16









Mr. Xcoder

30.6k758194




30.6k758194











  • Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
    – Arnauld
    Sep 16 at 13:17










  • Can we take the matrix size as input?
    – Delfad0r
    Sep 17 at 5:32










  • @Delfad0r I always forget to mention that. Yes, you may take the matrix size as input.
    – Mr. Xcoder
    Sep 17 at 7:19
















  • Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
    – Arnauld
    Sep 16 at 13:17










  • Can we take the matrix size as input?
    – Delfad0r
    Sep 17 at 5:32










  • @Delfad0r I always forget to mention that. Yes, you may take the matrix size as input.
    – Mr. Xcoder
    Sep 17 at 7:19















Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
– Arnauld
Sep 16 at 13:17




Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
– Arnauld
Sep 16 at 13:17












Can we take the matrix size as input?
– Delfad0r
Sep 17 at 5:32




Can we take the matrix size as input?
– Delfad0r
Sep 17 at 5:32












@Delfad0r I always forget to mention that. Yes, you may take the matrix size as input.
– Mr. Xcoder
Sep 17 at 7:19




@Delfad0r I always forget to mention that. Yes, you may take the matrix size as input.
– Mr. Xcoder
Sep 17 at 7:19










7 Answers
7






active

oldest

votes

















up vote
3
down vote














APL (Dyalog), 17 bytes



1=⊢∨(×/∘,↓)⌺3 3÷⊢


Try it online! (credits to ngn for translating the test cases to APL)



Brief explanation



(×/∘,↓)⌺3 3 gets the product of each element with its neighbours.



Then I divide by the argument ÷⊢, so that each entry in the matrix has been mapped to the product of its neighbors.



Finally I take the gcd of the argument with this matrix ⊢∨, and check for equality with 1, 1=



Note, as with ngn's answer, this fails for some inputs due to a bug in the interpreter.






share|improve this answer



























    up vote
    2
    down vote













    JavaScript (ES6), 121 bytes



    Returns a matrix of Boolean values, where false means hostile.





    m=>m.map((r,y)=>r.map((v,x)=>[...'12221000'].some((k,j,a)=>(g=(a,b)=>b?g(b,a%b):a>1)(v,(m[y+~-k]||0)[x+~-a[j+2&7]]||1))))


    Try it online!



    How?



    The method used to isolate the 8 neighbors of each cell is similar to the one I described here.



    Commented



    m => // m = input matrix
    m.map((r, y) => // for each row r at position y in m:
    r.map((v, x) => // for each value v at position x in r:
    [...'12221000'] // we consider all 8 neighbors
    .some((k, j, a) => // for each k at position j in this array a:
    ( g = (a, b) => // g is a function which takes 2 integers a and b
    b ? // and recursively determines whether they are
    g(b, a % b) // coprime to each other
    : // (returns false if they are, true if they're not)
    a > 1 //
    )( // initial call to g() with:
    v, // the value of the current cell
    (m[y + ~-k] || 0) // and the value of the current neighbor
    [x + ~-a[j + 2 & 7]] //
    || 1 // or 1 if this neighbor is undefined
    )))) // (to make sure it's coprime with v)





    share|improve this answer





























      up vote
      2
      down vote














      MATL, 22 bytes



      tTT1&Ya3thYC5&Y)Zd1=A)


      Input is a matrix. Output is all numbers with hostile neighbours.



      Try it online! Or verify all test cases.



      Explanation with worked example



      Consider input [38, 77, 11; 17, 51, 32; 66, 78, 19] as an example. Stack contents are shown bottom to top.



      t % Implicit input. Duplicate
      % STACK: [38, 77, 11;
      17, 51, 32;
      66, 78, 19]
      [38, 77, 11;
      17, 51, 32;
      66, 78, 19]
      TT1&Ya % Pad in the two dimensions with value 1 and width 1
      % STACK: [38, 77, 11;
      17, 51, 32;
      66, 78, 19]
      [1, 1, 1, 1, 1;
      1, 38, 77, 11, 1;
      1, 17, 51, 32, 1;
      1, 66, 78, 19, 1
      1, 1, 1, 1, 1]
      3thYC % Convert each sliding 3×3 block into a column (in column-major order)
      % STACK: [38, 77, 11;
      17, 51, 32;
      66, 78, 19]
      [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
      1, 1, 1, 38, 17, 66, 77, 51, 78;
      1, 1, 1, 17, 66, 1, 51, 78, 1;
      1, 38, 17, 1, 77, 51, 1, 11, 32;
      38, 17, 66, 77, 51, 78, 11, 32, 19;
      17, 66, 1, 51, 78, 1, 32, 19, 1;
      1, 77, 51, 1, 11, 32, 1, 1, 1;
      77, 51, 78, 11, 32, 19, 1, 1, 1;
      51, 78, 1, 32, 19, 1, 1, 1, 1]
      5&Y) % Push 5th row (centers of the 3×3 blocks) and then the rest of the matrix
      % STACK: [38, 77, 11;
      17, 51, 32;
      66, 78, 19]
      [38, 17, 66, 77, 51, 78, 11, 32, 19]
      [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
      1, 1, 1, 38, 17, 66, 77, 51, 78;
      1, 1, 1, 17, 66, 1, 51, 78, 1;
      1, 38, 17, 1, 77, 51, 1, 11, 32;
      17, 66, 1, 51, 78, 1, 32, 19, 1;
      1, 77, 51, 1, 11, 32, 1, 1, 1;
      77, 51, 78, 11, 32, 19, 1, 1, 1;
      51, 78, 1, 32, 19, 1, 1, 1, 1]
      Zd % Greatest common divisor, element-wise with broadcast
      % STACK: [38, 77, 11;
      17, 51, 32;
      66, 78, 19]
      [1, 1, 1, 1, 1, 1, 1, 1, 1;
      1, 1, 1, 1, 17, 6, 11, 1, 1;
      1, 1, 1, 1, 3, 1, 1, 2, 1;
      1, 1, 1, 1, 1, 3, 1, 1, 1;
      1, 1, 1, 1, 3, 1, 1, 1, 1;
      1, 1, 3, 1, 1, 2, 1, 1, 1;
      1, 17, 6, 11, 1, 1, 1, 1, 1;
      1, 1, 1, 1, 1, 1, 1, 1, 1]
      1= % Compare with 1, element-wise. Gives true (1) or false (0)
      % STACK: [38, 77, 11;
      17, 51, 32;
      66, 78, 19]
      [1, 1, 1, 1, 1, 1, 1, 1, 1;
      1, 1, 1, 1, 0, 0, 0, 1, 1;
      1, 1, 1, 1, 0, 1, 1, 0, 1;
      1, 1, 1, 1, 1, 0, 1, 1, 1;
      1, 1, 1, 1, 0, 1, 1, 1, 1;
      1, 1, 0, 1, 1, 0, 1, 1, 1;
      1, 0, 0, 0, 1, 1, 1, 1, 1;
      1, 1, 1, 1, 1, 1, 1, 1, 1]
      A % All: true (1) for columns that do not contain 0
      % STACK: [38, 77, 11;
      17, 51, 32;
      66, 78, 19]
      [1, 0, 0, 0, 0, 0, 0, 0, 1]
      ) % Index (the matrix is read in column-major order). Implicit display
      % [38, 19]





      share|improve this answer






















      • Will this work if the matrix is bigger than 3x3?
        – Robert Fraser
        Sep 17 at 4:42










      • @RobertFraser Yes, the procedure does not depend on matrix size. See last test case for example
        – Luis Mendo
        Sep 17 at 9:23

















      up vote
      1
      down vote














      APL (Dyalog Classic), 23 22 bytes



      -1 byte thanks to @H.PWiz





      ∧/1=1↓∨∘⊃⍨1⌈4⌽,⍵⌺3 3


      Try it online!



      doesn't support matrices smaller than 3x3 due to a bug in the interpreter






      share|improve this answer






















      • @H.PWiz that's very smart, do you wanna post it as your own?
        – ngn
        Sep 16 at 18:03










      • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
        – H.PWiz
        Sep 16 at 18:04

















      up vote
      1
      down vote














      Jelly, 24 bytes



      Hmm, seems long.



      ỊẠ€T
      ŒJ_€`Ç€ḟ"J$ịFg"FÇịF


      A monadic Link accepting a list of lists of positive integers which returns a list of each of the values which are in hostile neighbourhoods (version 1 with no de-duplication).



      Try it online! Or see a test-suite.



      How?



      ỊẠ€T - Link 1: indices of items which only contain "insignificant" values: list of lists
      Ị - insignificant (vectorises) -- 1 if (-1<=value<=1) else 0
      € - for €ach:
      Ạ - all?
      T - truthy indices

      ŒJ_€`Ç€ḟ"J$ịFg"FÇịF - Main Link: list of lists of positive integers, M
      Ã…Â’J - multi-dimensional indices
      ` - use as right argument as well as left...
      € - for €ach:
      _ - subtract (vectorises)
      € - for €ach:
      Ç - call last Link (1) as a monad
      $ - last two links as a monad:
      J - range of length -> [1,2,3,...,n(elements)]
      " - zip with:
      ḟ - filter discard (remove the index of the item itself)
      F - flatten M
      ị - index into (vectorises) -- getting a list of lists of neighbours
      F - flatten M
      " - zip with:
      g - greatest common divisor
      Ç - call last Link (1) as a monad
      F - flatten M
      ị - index into





      share|improve this answer





























        up vote
        1
        down vote














        Python 2, 182 177 166 bytes





        lambda a:[[all(gcd(t,a[i+v][j+h])<2for h in[-1,0,1]for v in[-1,0,1]if(h|v)*(i+v>-1<j+h<len(a)>i+v))for j,t in E(s)]for i,s in E(a)]
        from fractions import*
        E=enumerate


        Try it online!



        Outputs a list of lists with True/False entries.






        share|improve this answer





























          up vote
          1
          down vote














          Haskell, 95 bytes





          m?n|l<-[0..n-1]=[a|i<-l,j<-l,a<-[m!!i!!j],2>sum[1|u<-l,v<-l,(i-u)^2+(j-v)^2<4,gcd(m!!u!!v)a>1]]


          Try it online!



          The function ? takes the matrix m as a list of lists and the matrix size n; it returns the list of entries in hostility.






          share|improve this answer




















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "200"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172273%2fwhose-neighbours-are-hostile%23new-answer', 'question_page');

            );

            Post as a guest






























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote














            APL (Dyalog), 17 bytes



            1=⊢∨(×/∘,↓)⌺3 3÷⊢


            Try it online! (credits to ngn for translating the test cases to APL)



            Brief explanation



            (×/∘,↓)⌺3 3 gets the product of each element with its neighbours.



            Then I divide by the argument ÷⊢, so that each entry in the matrix has been mapped to the product of its neighbors.



            Finally I take the gcd of the argument with this matrix ⊢∨, and check for equality with 1, 1=



            Note, as with ngn's answer, this fails for some inputs due to a bug in the interpreter.






            share|improve this answer
























              up vote
              3
              down vote














              APL (Dyalog), 17 bytes



              1=⊢∨(×/∘,↓)⌺3 3÷⊢


              Try it online! (credits to ngn for translating the test cases to APL)



              Brief explanation



              (×/∘,↓)⌺3 3 gets the product of each element with its neighbours.



              Then I divide by the argument ÷⊢, so that each entry in the matrix has been mapped to the product of its neighbors.



              Finally I take the gcd of the argument with this matrix ⊢∨, and check for equality with 1, 1=



              Note, as with ngn's answer, this fails for some inputs due to a bug in the interpreter.






              share|improve this answer






















                up vote
                3
                down vote










                up vote
                3
                down vote










                APL (Dyalog), 17 bytes



                1=⊢∨(×/∘,↓)⌺3 3÷⊢


                Try it online! (credits to ngn for translating the test cases to APL)



                Brief explanation



                (×/∘,↓)⌺3 3 gets the product of each element with its neighbours.



                Then I divide by the argument ÷⊢, so that each entry in the matrix has been mapped to the product of its neighbors.



                Finally I take the gcd of the argument with this matrix ⊢∨, and check for equality with 1, 1=



                Note, as with ngn's answer, this fails for some inputs due to a bug in the interpreter.






                share|improve this answer













                APL (Dyalog), 17 bytes



                1=⊢∨(×/∘,↓)⌺3 3÷⊢


                Try it online! (credits to ngn for translating the test cases to APL)



                Brief explanation



                (×/∘,↓)⌺3 3 gets the product of each element with its neighbours.



                Then I divide by the argument ÷⊢, so that each entry in the matrix has been mapped to the product of its neighbors.



                Finally I take the gcd of the argument with this matrix ⊢∨, and check for equality with 1, 1=



                Note, as with ngn's answer, this fails for some inputs due to a bug in the interpreter.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 16 at 18:15









                H.PWiz

                9,75921249




                9,75921249




















                    up vote
                    2
                    down vote













                    JavaScript (ES6), 121 bytes



                    Returns a matrix of Boolean values, where false means hostile.





                    m=>m.map((r,y)=>r.map((v,x)=>[...'12221000'].some((k,j,a)=>(g=(a,b)=>b?g(b,a%b):a>1)(v,(m[y+~-k]||0)[x+~-a[j+2&7]]||1))))


                    Try it online!



                    How?



                    The method used to isolate the 8 neighbors of each cell is similar to the one I described here.



                    Commented



                    m => // m = input matrix
                    m.map((r, y) => // for each row r at position y in m:
                    r.map((v, x) => // for each value v at position x in r:
                    [...'12221000'] // we consider all 8 neighbors
                    .some((k, j, a) => // for each k at position j in this array a:
                    ( g = (a, b) => // g is a function which takes 2 integers a and b
                    b ? // and recursively determines whether they are
                    g(b, a % b) // coprime to each other
                    : // (returns false if they are, true if they're not)
                    a > 1 //
                    )( // initial call to g() with:
                    v, // the value of the current cell
                    (m[y + ~-k] || 0) // and the value of the current neighbor
                    [x + ~-a[j + 2 & 7]] //
                    || 1 // or 1 if this neighbor is undefined
                    )))) // (to make sure it's coprime with v)





                    share|improve this answer


























                      up vote
                      2
                      down vote













                      JavaScript (ES6), 121 bytes



                      Returns a matrix of Boolean values, where false means hostile.





                      m=>m.map((r,y)=>r.map((v,x)=>[...'12221000'].some((k,j,a)=>(g=(a,b)=>b?g(b,a%b):a>1)(v,(m[y+~-k]||0)[x+~-a[j+2&7]]||1))))


                      Try it online!



                      How?



                      The method used to isolate the 8 neighbors of each cell is similar to the one I described here.



                      Commented



                      m => // m = input matrix
                      m.map((r, y) => // for each row r at position y in m:
                      r.map((v, x) => // for each value v at position x in r:
                      [...'12221000'] // we consider all 8 neighbors
                      .some((k, j, a) => // for each k at position j in this array a:
                      ( g = (a, b) => // g is a function which takes 2 integers a and b
                      b ? // and recursively determines whether they are
                      g(b, a % b) // coprime to each other
                      : // (returns false if they are, true if they're not)
                      a > 1 //
                      )( // initial call to g() with:
                      v, // the value of the current cell
                      (m[y + ~-k] || 0) // and the value of the current neighbor
                      [x + ~-a[j + 2 & 7]] //
                      || 1 // or 1 if this neighbor is undefined
                      )))) // (to make sure it's coprime with v)





                      share|improve this answer
























                        up vote
                        2
                        down vote










                        up vote
                        2
                        down vote









                        JavaScript (ES6), 121 bytes



                        Returns a matrix of Boolean values, where false means hostile.





                        m=>m.map((r,y)=>r.map((v,x)=>[...'12221000'].some((k,j,a)=>(g=(a,b)=>b?g(b,a%b):a>1)(v,(m[y+~-k]||0)[x+~-a[j+2&7]]||1))))


                        Try it online!



                        How?



                        The method used to isolate the 8 neighbors of each cell is similar to the one I described here.



                        Commented



                        m => // m = input matrix
                        m.map((r, y) => // for each row r at position y in m:
                        r.map((v, x) => // for each value v at position x in r:
                        [...'12221000'] // we consider all 8 neighbors
                        .some((k, j, a) => // for each k at position j in this array a:
                        ( g = (a, b) => // g is a function which takes 2 integers a and b
                        b ? // and recursively determines whether they are
                        g(b, a % b) // coprime to each other
                        : // (returns false if they are, true if they're not)
                        a > 1 //
                        )( // initial call to g() with:
                        v, // the value of the current cell
                        (m[y + ~-k] || 0) // and the value of the current neighbor
                        [x + ~-a[j + 2 & 7]] //
                        || 1 // or 1 if this neighbor is undefined
                        )))) // (to make sure it's coprime with v)





                        share|improve this answer














                        JavaScript (ES6), 121 bytes



                        Returns a matrix of Boolean values, where false means hostile.





                        m=>m.map((r,y)=>r.map((v,x)=>[...'12221000'].some((k,j,a)=>(g=(a,b)=>b?g(b,a%b):a>1)(v,(m[y+~-k]||0)[x+~-a[j+2&7]]||1))))


                        Try it online!



                        How?



                        The method used to isolate the 8 neighbors of each cell is similar to the one I described here.



                        Commented



                        m => // m = input matrix
                        m.map((r, y) => // for each row r at position y in m:
                        r.map((v, x) => // for each value v at position x in r:
                        [...'12221000'] // we consider all 8 neighbors
                        .some((k, j, a) => // for each k at position j in this array a:
                        ( g = (a, b) => // g is a function which takes 2 integers a and b
                        b ? // and recursively determines whether they are
                        g(b, a % b) // coprime to each other
                        : // (returns false if they are, true if they're not)
                        a > 1 //
                        )( // initial call to g() with:
                        v, // the value of the current cell
                        (m[y + ~-k] || 0) // and the value of the current neighbor
                        [x + ~-a[j + 2 & 7]] //
                        || 1 // or 1 if this neighbor is undefined
                        )))) // (to make sure it's coprime with v)






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Sep 16 at 16:54

























                        answered Sep 16 at 12:40









                        Arnauld

                        65.5k583277




                        65.5k583277




















                            up vote
                            2
                            down vote














                            MATL, 22 bytes



                            tTT1&Ya3thYC5&Y)Zd1=A)


                            Input is a matrix. Output is all numbers with hostile neighbours.



                            Try it online! Or verify all test cases.



                            Explanation with worked example



                            Consider input [38, 77, 11; 17, 51, 32; 66, 78, 19] as an example. Stack contents are shown bottom to top.



                            t % Implicit input. Duplicate
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            TT1&Ya % Pad in the two dimensions with value 1 and width 1
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1;
                            1, 38, 77, 11, 1;
                            1, 17, 51, 32, 1;
                            1, 66, 78, 19, 1
                            1, 1, 1, 1, 1]
                            3thYC % Convert each sliding 3×3 block into a column (in column-major order)
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
                            1, 1, 1, 38, 17, 66, 77, 51, 78;
                            1, 1, 1, 17, 66, 1, 51, 78, 1;
                            1, 38, 17, 1, 77, 51, 1, 11, 32;
                            38, 17, 66, 77, 51, 78, 11, 32, 19;
                            17, 66, 1, 51, 78, 1, 32, 19, 1;
                            1, 77, 51, 1, 11, 32, 1, 1, 1;
                            77, 51, 78, 11, 32, 19, 1, 1, 1;
                            51, 78, 1, 32, 19, 1, 1, 1, 1]
                            5&Y) % Push 5th row (centers of the 3×3 blocks) and then the rest of the matrix
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [38, 17, 66, 77, 51, 78, 11, 32, 19]
                            [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
                            1, 1, 1, 38, 17, 66, 77, 51, 78;
                            1, 1, 1, 17, 66, 1, 51, 78, 1;
                            1, 38, 17, 1, 77, 51, 1, 11, 32;
                            17, 66, 1, 51, 78, 1, 32, 19, 1;
                            1, 77, 51, 1, 11, 32, 1, 1, 1;
                            77, 51, 78, 11, 32, 19, 1, 1, 1;
                            51, 78, 1, 32, 19, 1, 1, 1, 1]
                            Zd % Greatest common divisor, element-wise with broadcast
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 17, 6, 11, 1, 1;
                            1, 1, 1, 1, 3, 1, 1, 2, 1;
                            1, 1, 1, 1, 1, 3, 1, 1, 1;
                            1, 1, 1, 1, 3, 1, 1, 1, 1;
                            1, 1, 3, 1, 1, 2, 1, 1, 1;
                            1, 17, 6, 11, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 1, 1, 1, 1, 1]
                            1= % Compare with 1, element-wise. Gives true (1) or false (0)
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 0, 0, 0, 1, 1;
                            1, 1, 1, 1, 0, 1, 1, 0, 1;
                            1, 1, 1, 1, 1, 0, 1, 1, 1;
                            1, 1, 1, 1, 0, 1, 1, 1, 1;
                            1, 1, 0, 1, 1, 0, 1, 1, 1;
                            1, 0, 0, 0, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 1, 1, 1, 1, 1]
                            A % All: true (1) for columns that do not contain 0
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 0, 0, 0, 0, 0, 0, 0, 1]
                            ) % Index (the matrix is read in column-major order). Implicit display
                            % [38, 19]





                            share|improve this answer






















                            • Will this work if the matrix is bigger than 3x3?
                              – Robert Fraser
                              Sep 17 at 4:42










                            • @RobertFraser Yes, the procedure does not depend on matrix size. See last test case for example
                              – Luis Mendo
                              Sep 17 at 9:23














                            up vote
                            2
                            down vote














                            MATL, 22 bytes



                            tTT1&Ya3thYC5&Y)Zd1=A)


                            Input is a matrix. Output is all numbers with hostile neighbours.



                            Try it online! Or verify all test cases.



                            Explanation with worked example



                            Consider input [38, 77, 11; 17, 51, 32; 66, 78, 19] as an example. Stack contents are shown bottom to top.



                            t % Implicit input. Duplicate
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            TT1&Ya % Pad in the two dimensions with value 1 and width 1
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1;
                            1, 38, 77, 11, 1;
                            1, 17, 51, 32, 1;
                            1, 66, 78, 19, 1
                            1, 1, 1, 1, 1]
                            3thYC % Convert each sliding 3×3 block into a column (in column-major order)
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
                            1, 1, 1, 38, 17, 66, 77, 51, 78;
                            1, 1, 1, 17, 66, 1, 51, 78, 1;
                            1, 38, 17, 1, 77, 51, 1, 11, 32;
                            38, 17, 66, 77, 51, 78, 11, 32, 19;
                            17, 66, 1, 51, 78, 1, 32, 19, 1;
                            1, 77, 51, 1, 11, 32, 1, 1, 1;
                            77, 51, 78, 11, 32, 19, 1, 1, 1;
                            51, 78, 1, 32, 19, 1, 1, 1, 1]
                            5&Y) % Push 5th row (centers of the 3×3 blocks) and then the rest of the matrix
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [38, 17, 66, 77, 51, 78, 11, 32, 19]
                            [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
                            1, 1, 1, 38, 17, 66, 77, 51, 78;
                            1, 1, 1, 17, 66, 1, 51, 78, 1;
                            1, 38, 17, 1, 77, 51, 1, 11, 32;
                            17, 66, 1, 51, 78, 1, 32, 19, 1;
                            1, 77, 51, 1, 11, 32, 1, 1, 1;
                            77, 51, 78, 11, 32, 19, 1, 1, 1;
                            51, 78, 1, 32, 19, 1, 1, 1, 1]
                            Zd % Greatest common divisor, element-wise with broadcast
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 17, 6, 11, 1, 1;
                            1, 1, 1, 1, 3, 1, 1, 2, 1;
                            1, 1, 1, 1, 1, 3, 1, 1, 1;
                            1, 1, 1, 1, 3, 1, 1, 1, 1;
                            1, 1, 3, 1, 1, 2, 1, 1, 1;
                            1, 17, 6, 11, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 1, 1, 1, 1, 1]
                            1= % Compare with 1, element-wise. Gives true (1) or false (0)
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 0, 0, 0, 1, 1;
                            1, 1, 1, 1, 0, 1, 1, 0, 1;
                            1, 1, 1, 1, 1, 0, 1, 1, 1;
                            1, 1, 1, 1, 0, 1, 1, 1, 1;
                            1, 1, 0, 1, 1, 0, 1, 1, 1;
                            1, 0, 0, 0, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 1, 1, 1, 1, 1]
                            A % All: true (1) for columns that do not contain 0
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 0, 0, 0, 0, 0, 0, 0, 1]
                            ) % Index (the matrix is read in column-major order). Implicit display
                            % [38, 19]





                            share|improve this answer






















                            • Will this work if the matrix is bigger than 3x3?
                              – Robert Fraser
                              Sep 17 at 4:42










                            • @RobertFraser Yes, the procedure does not depend on matrix size. See last test case for example
                              – Luis Mendo
                              Sep 17 at 9:23












                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote










                            MATL, 22 bytes



                            tTT1&Ya3thYC5&Y)Zd1=A)


                            Input is a matrix. Output is all numbers with hostile neighbours.



                            Try it online! Or verify all test cases.



                            Explanation with worked example



                            Consider input [38, 77, 11; 17, 51, 32; 66, 78, 19] as an example. Stack contents are shown bottom to top.



                            t % Implicit input. Duplicate
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            TT1&Ya % Pad in the two dimensions with value 1 and width 1
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1;
                            1, 38, 77, 11, 1;
                            1, 17, 51, 32, 1;
                            1, 66, 78, 19, 1
                            1, 1, 1, 1, 1]
                            3thYC % Convert each sliding 3×3 block into a column (in column-major order)
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
                            1, 1, 1, 38, 17, 66, 77, 51, 78;
                            1, 1, 1, 17, 66, 1, 51, 78, 1;
                            1, 38, 17, 1, 77, 51, 1, 11, 32;
                            38, 17, 66, 77, 51, 78, 11, 32, 19;
                            17, 66, 1, 51, 78, 1, 32, 19, 1;
                            1, 77, 51, 1, 11, 32, 1, 1, 1;
                            77, 51, 78, 11, 32, 19, 1, 1, 1;
                            51, 78, 1, 32, 19, 1, 1, 1, 1]
                            5&Y) % Push 5th row (centers of the 3×3 blocks) and then the rest of the matrix
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [38, 17, 66, 77, 51, 78, 11, 32, 19]
                            [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
                            1, 1, 1, 38, 17, 66, 77, 51, 78;
                            1, 1, 1, 17, 66, 1, 51, 78, 1;
                            1, 38, 17, 1, 77, 51, 1, 11, 32;
                            17, 66, 1, 51, 78, 1, 32, 19, 1;
                            1, 77, 51, 1, 11, 32, 1, 1, 1;
                            77, 51, 78, 11, 32, 19, 1, 1, 1;
                            51, 78, 1, 32, 19, 1, 1, 1, 1]
                            Zd % Greatest common divisor, element-wise with broadcast
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 17, 6, 11, 1, 1;
                            1, 1, 1, 1, 3, 1, 1, 2, 1;
                            1, 1, 1, 1, 1, 3, 1, 1, 1;
                            1, 1, 1, 1, 3, 1, 1, 1, 1;
                            1, 1, 3, 1, 1, 2, 1, 1, 1;
                            1, 17, 6, 11, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 1, 1, 1, 1, 1]
                            1= % Compare with 1, element-wise. Gives true (1) or false (0)
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 0, 0, 0, 1, 1;
                            1, 1, 1, 1, 0, 1, 1, 0, 1;
                            1, 1, 1, 1, 1, 0, 1, 1, 1;
                            1, 1, 1, 1, 0, 1, 1, 1, 1;
                            1, 1, 0, 1, 1, 0, 1, 1, 1;
                            1, 0, 0, 0, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 1, 1, 1, 1, 1]
                            A % All: true (1) for columns that do not contain 0
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 0, 0, 0, 0, 0, 0, 0, 1]
                            ) % Index (the matrix is read in column-major order). Implicit display
                            % [38, 19]





                            share|improve this answer















                            MATL, 22 bytes



                            tTT1&Ya3thYC5&Y)Zd1=A)


                            Input is a matrix. Output is all numbers with hostile neighbours.



                            Try it online! Or verify all test cases.



                            Explanation with worked example



                            Consider input [38, 77, 11; 17, 51, 32; 66, 78, 19] as an example. Stack contents are shown bottom to top.



                            t % Implicit input. Duplicate
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            TT1&Ya % Pad in the two dimensions with value 1 and width 1
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1;
                            1, 38, 77, 11, 1;
                            1, 17, 51, 32, 1;
                            1, 66, 78, 19, 1
                            1, 1, 1, 1, 1]
                            3thYC % Convert each sliding 3×3 block into a column (in column-major order)
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
                            1, 1, 1, 38, 17, 66, 77, 51, 78;
                            1, 1, 1, 17, 66, 1, 51, 78, 1;
                            1, 38, 17, 1, 77, 51, 1, 11, 32;
                            38, 17, 66, 77, 51, 78, 11, 32, 19;
                            17, 66, 1, 51, 78, 1, 32, 19, 1;
                            1, 77, 51, 1, 11, 32, 1, 1, 1;
                            77, 51, 78, 11, 32, 19, 1, 1, 1;
                            51, 78, 1, 32, 19, 1, 1, 1, 1]
                            5&Y) % Push 5th row (centers of the 3×3 blocks) and then the rest of the matrix
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [38, 17, 66, 77, 51, 78, 11, 32, 19]
                            [ 1, 1, 1, 1, 38, 17, 1, 77, 51;
                            1, 1, 1, 38, 17, 66, 77, 51, 78;
                            1, 1, 1, 17, 66, 1, 51, 78, 1;
                            1, 38, 17, 1, 77, 51, 1, 11, 32;
                            17, 66, 1, 51, 78, 1, 32, 19, 1;
                            1, 77, 51, 1, 11, 32, 1, 1, 1;
                            77, 51, 78, 11, 32, 19, 1, 1, 1;
                            51, 78, 1, 32, 19, 1, 1, 1, 1]
                            Zd % Greatest common divisor, element-wise with broadcast
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 17, 6, 11, 1, 1;
                            1, 1, 1, 1, 3, 1, 1, 2, 1;
                            1, 1, 1, 1, 1, 3, 1, 1, 1;
                            1, 1, 1, 1, 3, 1, 1, 1, 1;
                            1, 1, 3, 1, 1, 2, 1, 1, 1;
                            1, 17, 6, 11, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 1, 1, 1, 1, 1]
                            1= % Compare with 1, element-wise. Gives true (1) or false (0)
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 1, 1, 1, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 0, 0, 0, 1, 1;
                            1, 1, 1, 1, 0, 1, 1, 0, 1;
                            1, 1, 1, 1, 1, 0, 1, 1, 1;
                            1, 1, 1, 1, 0, 1, 1, 1, 1;
                            1, 1, 0, 1, 1, 0, 1, 1, 1;
                            1, 0, 0, 0, 1, 1, 1, 1, 1;
                            1, 1, 1, 1, 1, 1, 1, 1, 1]
                            A % All: true (1) for columns that do not contain 0
                            % STACK: [38, 77, 11;
                            17, 51, 32;
                            66, 78, 19]
                            [1, 0, 0, 0, 0, 0, 0, 0, 1]
                            ) % Index (the matrix is read in column-major order). Implicit display
                            % [38, 19]






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Sep 17 at 13:28

























                            answered Sep 16 at 21:46









                            Luis Mendo

                            72.8k885284




                            72.8k885284











                            • Will this work if the matrix is bigger than 3x3?
                              – Robert Fraser
                              Sep 17 at 4:42










                            • @RobertFraser Yes, the procedure does not depend on matrix size. See last test case for example
                              – Luis Mendo
                              Sep 17 at 9:23
















                            • Will this work if the matrix is bigger than 3x3?
                              – Robert Fraser
                              Sep 17 at 4:42










                            • @RobertFraser Yes, the procedure does not depend on matrix size. See last test case for example
                              – Luis Mendo
                              Sep 17 at 9:23















                            Will this work if the matrix is bigger than 3x3?
                            – Robert Fraser
                            Sep 17 at 4:42




                            Will this work if the matrix is bigger than 3x3?
                            – Robert Fraser
                            Sep 17 at 4:42












                            @RobertFraser Yes, the procedure does not depend on matrix size. See last test case for example
                            – Luis Mendo
                            Sep 17 at 9:23




                            @RobertFraser Yes, the procedure does not depend on matrix size. See last test case for example
                            – Luis Mendo
                            Sep 17 at 9:23










                            up vote
                            1
                            down vote














                            APL (Dyalog Classic), 23 22 bytes



                            -1 byte thanks to @H.PWiz





                            ∧/1=1↓∨∘⊃⍨1⌈4⌽,⍵⌺3 3


                            Try it online!



                            doesn't support matrices smaller than 3x3 due to a bug in the interpreter






                            share|improve this answer






















                            • @H.PWiz that's very smart, do you wanna post it as your own?
                              – ngn
                              Sep 16 at 18:03










                            • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                              – H.PWiz
                              Sep 16 at 18:04














                            up vote
                            1
                            down vote














                            APL (Dyalog Classic), 23 22 bytes



                            -1 byte thanks to @H.PWiz





                            ∧/1=1↓∨∘⊃⍨1⌈4⌽,⍵⌺3 3


                            Try it online!



                            doesn't support matrices smaller than 3x3 due to a bug in the interpreter






                            share|improve this answer






















                            • @H.PWiz that's very smart, do you wanna post it as your own?
                              – ngn
                              Sep 16 at 18:03










                            • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                              – H.PWiz
                              Sep 16 at 18:04












                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote










                            APL (Dyalog Classic), 23 22 bytes



                            -1 byte thanks to @H.PWiz





                            ∧/1=1↓∨∘⊃⍨1⌈4⌽,⍵⌺3 3


                            Try it online!



                            doesn't support matrices smaller than 3x3 due to a bug in the interpreter






                            share|improve this answer















                            APL (Dyalog Classic), 23 22 bytes



                            -1 byte thanks to @H.PWiz





                            ∧/1=1↓∨∘⊃⍨1⌈4⌽,⍵⌺3 3


                            Try it online!



                            doesn't support matrices smaller than 3x3 due to a bug in the interpreter







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Sep 16 at 18:19

























                            answered Sep 16 at 12:52









                            ngn

                            6,29812358




                            6,29812358











                            • @H.PWiz that's very smart, do you wanna post it as your own?
                              – ngn
                              Sep 16 at 18:03










                            • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                              – H.PWiz
                              Sep 16 at 18:04
















                            • @H.PWiz that's very smart, do you wanna post it as your own?
                              – ngn
                              Sep 16 at 18:03










                            • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                              – H.PWiz
                              Sep 16 at 18:04















                            @H.PWiz that's very smart, do you wanna post it as your own?
                            – ngn
                            Sep 16 at 18:03




                            @H.PWiz that's very smart, do you wanna post it as your own?
                            – ngn
                            Sep 16 at 18:03












                            Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                            – H.PWiz
                            Sep 16 at 18:04




                            Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                            – H.PWiz
                            Sep 16 at 18:04










                            up vote
                            1
                            down vote














                            Jelly, 24 bytes



                            Hmm, seems long.



                            ỊẠ€T
                            ŒJ_€`Ç€ḟ"J$ịFg"FÇịF


                            A monadic Link accepting a list of lists of positive integers which returns a list of each of the values which are in hostile neighbourhoods (version 1 with no de-duplication).



                            Try it online! Or see a test-suite.



                            How?



                            ỊẠ€T - Link 1: indices of items which only contain "insignificant" values: list of lists
                            Ị - insignificant (vectorises) -- 1 if (-1<=value<=1) else 0
                            € - for €ach:
                            Ạ - all?
                            T - truthy indices

                            ŒJ_€`Ç€ḟ"J$ịFg"FÇịF - Main Link: list of lists of positive integers, M
                            Ã…Â’J - multi-dimensional indices
                            ` - use as right argument as well as left...
                            € - for €ach:
                            _ - subtract (vectorises)
                            € - for €ach:
                            Ç - call last Link (1) as a monad
                            $ - last two links as a monad:
                            J - range of length -> [1,2,3,...,n(elements)]
                            " - zip with:
                            ḟ - filter discard (remove the index of the item itself)
                            F - flatten M
                            ị - index into (vectorises) -- getting a list of lists of neighbours
                            F - flatten M
                            " - zip with:
                            g - greatest common divisor
                            Ç - call last Link (1) as a monad
                            F - flatten M
                            ị - index into





                            share|improve this answer


























                              up vote
                              1
                              down vote














                              Jelly, 24 bytes



                              Hmm, seems long.



                              ỊẠ€T
                              ŒJ_€`Ç€ḟ"J$ịFg"FÇịF


                              A monadic Link accepting a list of lists of positive integers which returns a list of each of the values which are in hostile neighbourhoods (version 1 with no de-duplication).



                              Try it online! Or see a test-suite.



                              How?



                              ỊẠ€T - Link 1: indices of items which only contain "insignificant" values: list of lists
                              Ị - insignificant (vectorises) -- 1 if (-1<=value<=1) else 0
                              € - for €ach:
                              Ạ - all?
                              T - truthy indices

                              ŒJ_€`Ç€ḟ"J$ịFg"FÇịF - Main Link: list of lists of positive integers, M
                              Ã…Â’J - multi-dimensional indices
                              ` - use as right argument as well as left...
                              € - for €ach:
                              _ - subtract (vectorises)
                              € - for €ach:
                              Ç - call last Link (1) as a monad
                              $ - last two links as a monad:
                              J - range of length -> [1,2,3,...,n(elements)]
                              " - zip with:
                              ḟ - filter discard (remove the index of the item itself)
                              F - flatten M
                              ị - index into (vectorises) -- getting a list of lists of neighbours
                              F - flatten M
                              " - zip with:
                              g - greatest common divisor
                              Ç - call last Link (1) as a monad
                              F - flatten M
                              ị - index into





                              share|improve this answer
























                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote










                                Jelly, 24 bytes



                                Hmm, seems long.



                                ỊẠ€T
                                ŒJ_€`Ç€ḟ"J$ịFg"FÇịF


                                A monadic Link accepting a list of lists of positive integers which returns a list of each of the values which are in hostile neighbourhoods (version 1 with no de-duplication).



                                Try it online! Or see a test-suite.



                                How?



                                ỊẠ€T - Link 1: indices of items which only contain "insignificant" values: list of lists
                                Ị - insignificant (vectorises) -- 1 if (-1<=value<=1) else 0
                                € - for €ach:
                                Ạ - all?
                                T - truthy indices

                                ŒJ_€`Ç€ḟ"J$ịFg"FÇịF - Main Link: list of lists of positive integers, M
                                Ã…Â’J - multi-dimensional indices
                                ` - use as right argument as well as left...
                                € - for €ach:
                                _ - subtract (vectorises)
                                € - for €ach:
                                Ç - call last Link (1) as a monad
                                $ - last two links as a monad:
                                J - range of length -> [1,2,3,...,n(elements)]
                                " - zip with:
                                ḟ - filter discard (remove the index of the item itself)
                                F - flatten M
                                ị - index into (vectorises) -- getting a list of lists of neighbours
                                F - flatten M
                                " - zip with:
                                g - greatest common divisor
                                Ç - call last Link (1) as a monad
                                F - flatten M
                                ị - index into





                                share|improve this answer















                                Jelly, 24 bytes



                                Hmm, seems long.



                                ỊẠ€T
                                ŒJ_€`Ç€ḟ"J$ịFg"FÇịF


                                A monadic Link accepting a list of lists of positive integers which returns a list of each of the values which are in hostile neighbourhoods (version 1 with no de-duplication).



                                Try it online! Or see a test-suite.



                                How?



                                ỊẠ€T - Link 1: indices of items which only contain "insignificant" values: list of lists
                                Ị - insignificant (vectorises) -- 1 if (-1<=value<=1) else 0
                                € - for €ach:
                                Ạ - all?
                                T - truthy indices

                                ŒJ_€`Ç€ḟ"J$ịFg"FÇịF - Main Link: list of lists of positive integers, M
                                Ã…Â’J - multi-dimensional indices
                                ` - use as right argument as well as left...
                                € - for €ach:
                                _ - subtract (vectorises)
                                € - for €ach:
                                Ç - call last Link (1) as a monad
                                $ - last two links as a monad:
                                J - range of length -> [1,2,3,...,n(elements)]
                                " - zip with:
                                ḟ - filter discard (remove the index of the item itself)
                                F - flatten M
                                ị - index into (vectorises) -- getting a list of lists of neighbours
                                F - flatten M
                                " - zip with:
                                g - greatest common divisor
                                Ç - call last Link (1) as a monad
                                F - flatten M
                                ị - index into






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Sep 16 at 18:23

























                                answered Sep 16 at 18:03









                                Jonathan Allan

                                48.6k534160




                                48.6k534160




















                                    up vote
                                    1
                                    down vote














                                    Python 2, 182 177 166 bytes





                                    lambda a:[[all(gcd(t,a[i+v][j+h])<2for h in[-1,0,1]for v in[-1,0,1]if(h|v)*(i+v>-1<j+h<len(a)>i+v))for j,t in E(s)]for i,s in E(a)]
                                    from fractions import*
                                    E=enumerate


                                    Try it online!



                                    Outputs a list of lists with True/False entries.






                                    share|improve this answer


























                                      up vote
                                      1
                                      down vote














                                      Python 2, 182 177 166 bytes





                                      lambda a:[[all(gcd(t,a[i+v][j+h])<2for h in[-1,0,1]for v in[-1,0,1]if(h|v)*(i+v>-1<j+h<len(a)>i+v))for j,t in E(s)]for i,s in E(a)]
                                      from fractions import*
                                      E=enumerate


                                      Try it online!



                                      Outputs a list of lists with True/False entries.






                                      share|improve this answer
























                                        up vote
                                        1
                                        down vote










                                        up vote
                                        1
                                        down vote










                                        Python 2, 182 177 166 bytes





                                        lambda a:[[all(gcd(t,a[i+v][j+h])<2for h in[-1,0,1]for v in[-1,0,1]if(h|v)*(i+v>-1<j+h<len(a)>i+v))for j,t in E(s)]for i,s in E(a)]
                                        from fractions import*
                                        E=enumerate


                                        Try it online!



                                        Outputs a list of lists with True/False entries.






                                        share|improve this answer















                                        Python 2, 182 177 166 bytes





                                        lambda a:[[all(gcd(t,a[i+v][j+h])<2for h in[-1,0,1]for v in[-1,0,1]if(h|v)*(i+v>-1<j+h<len(a)>i+v))for j,t in E(s)]for i,s in E(a)]
                                        from fractions import*
                                        E=enumerate


                                        Try it online!



                                        Outputs a list of lists with True/False entries.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Sep 17 at 3:31

























                                        answered Sep 16 at 19:59









                                        Chas Brown

                                        4,3111319




                                        4,3111319




















                                            up vote
                                            1
                                            down vote














                                            Haskell, 95 bytes





                                            m?n|l<-[0..n-1]=[a|i<-l,j<-l,a<-[m!!i!!j],2>sum[1|u<-l,v<-l,(i-u)^2+(j-v)^2<4,gcd(m!!u!!v)a>1]]


                                            Try it online!



                                            The function ? takes the matrix m as a list of lists and the matrix size n; it returns the list of entries in hostility.






                                            share|improve this answer
























                                              up vote
                                              1
                                              down vote














                                              Haskell, 95 bytes





                                              m?n|l<-[0..n-1]=[a|i<-l,j<-l,a<-[m!!i!!j],2>sum[1|u<-l,v<-l,(i-u)^2+(j-v)^2<4,gcd(m!!u!!v)a>1]]


                                              Try it online!



                                              The function ? takes the matrix m as a list of lists and the matrix size n; it returns the list of entries in hostility.






                                              share|improve this answer






















                                                up vote
                                                1
                                                down vote










                                                up vote
                                                1
                                                down vote










                                                Haskell, 95 bytes





                                                m?n|l<-[0..n-1]=[a|i<-l,j<-l,a<-[m!!i!!j],2>sum[1|u<-l,v<-l,(i-u)^2+(j-v)^2<4,gcd(m!!u!!v)a>1]]


                                                Try it online!



                                                The function ? takes the matrix m as a list of lists and the matrix size n; it returns the list of entries in hostility.






                                                share|improve this answer













                                                Haskell, 95 bytes





                                                m?n|l<-[0..n-1]=[a|i<-l,j<-l,a<-[m!!i!!j],2>sum[1|u<-l,v<-l,(i-u)^2+(j-v)^2<4,gcd(m!!u!!v)a>1]]


                                                Try it online!



                                                The function ? takes the matrix m as a list of lists and the matrix size n; it returns the list of entries in hostility.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Sep 18 at 7:57









                                                Delfad0r

                                                768213




                                                768213



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172273%2fwhose-neighbours-are-hostile%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest













































































                                                    Popular posts from this blog

                                                    How to check contact read email or not when send email to Individual?

                                                    Bahrain

                                                    Postfix configuration issue with fips on centos 7; mailgun relay