Whose neighbours are hostile?

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











up vote
5
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!



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
    5 hours ago






  • 1




    @JonathanAllan Done, thanks!
    – Mr. Xcoder
    2 hours ago















up vote
5
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!



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
    5 hours ago






  • 1




    @JonathanAllan Done, thanks!
    – Mr. Xcoder
    2 hours ago













up vote
5
down vote

favorite









up vote
5
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!



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!



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 2 hours ago

























asked 6 hours ago









Mr. Xcoder

30.4k758193




30.4k758193











  • Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
    – Arnauld
    5 hours ago






  • 1




    @JonathanAllan Done, thanks!
    – Mr. Xcoder
    2 hours ago

















  • Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
    – Arnauld
    5 hours ago






  • 1




    @JonathanAllan Done, thanks!
    – Mr. Xcoder
    2 hours ago
















Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
– Arnauld
5 hours ago




Borrowing stuff from hostile neighbors? For some reason, this reminds me of Jeff Minter's game Hover Bovver...
– Arnauld
5 hours ago




1




1




@JonathanAllan Done, thanks!
– Mr. Xcoder
2 hours ago





@JonathanAllan Done, thanks!
– Mr. Xcoder
2 hours ago











4 Answers
4






active

oldest

votes

















up vote
1
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
    1
    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
      0
      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
        41 mins ago










      • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
        – H.PWiz
        40 mins ago

















      up vote
      0
      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






















        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






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        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
          1
          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
            1
            down vote










            up vote
            1
            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 1 hour ago

























            answered 6 hours ago









            Arnauld

            64.2k580270




            64.2k580270




















                up vote
                1
                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
                  1
                  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
                    1
                    down vote










                    up vote
                    1
                    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 29 mins ago









                    H.PWiz

                    9,68921249




                    9,68921249




















                        up vote
                        0
                        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
                          41 mins ago










                        • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                          – H.PWiz
                          40 mins ago














                        up vote
                        0
                        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
                          41 mins ago










                        • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                          – H.PWiz
                          40 mins ago












                        up vote
                        0
                        down vote










                        up vote
                        0
                        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 25 mins ago

























                        answered 5 hours ago









                        ngn

                        6,19812256




                        6,19812256











                        • @H.PWiz that's very smart, do you wanna post it as your own?
                          – ngn
                          41 mins ago










                        • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                          – H.PWiz
                          40 mins ago
















                        • @H.PWiz that's very smart, do you wanna post it as your own?
                          – ngn
                          41 mins ago










                        • Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                          – H.PWiz
                          40 mins ago















                        @H.PWiz that's very smart, do you wanna post it as your own?
                        – ngn
                        41 mins ago




                        @H.PWiz that's very smart, do you wanna post it as your own?
                        – ngn
                        41 mins ago












                        Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                        – H.PWiz
                        40 mins ago




                        Sure, you can also use (⊃∨⊢) -> ∨∘⊂⍨ I think
                        – H.PWiz
                        40 mins ago










                        up vote
                        0
                        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
                          0
                          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
                            0
                            down vote










                            up vote
                            0
                            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 21 mins ago

























                            answered 41 mins ago









                            Jonathan Allan

                            48.3k534159




                            48.3k534159



























                                 

                                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













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                Installing NextGIS Connect into QGIS 3?

                                One-line joke