Whose neighbours are hostile?
Clash 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)]
code-golf array-manipulation arithmetic integer matrix
add a comment |Â
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)]
code-golf array-manipulation arithmetic integer matrix
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
add a comment |Â
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)]
code-golf array-manipulation arithmetic integer matrix
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
code-golf array-manipulation arithmetic integer matrix
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
add a comment |Â
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
add a comment |Â
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)
add a comment |Â
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.
add a comment |Â
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
@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
add a comment |Â
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
add a comment |Â
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)
add a comment |Â
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)
add a comment |Â
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)
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)
edited 1 hour ago
answered 6 hours ago
Arnauld
64.2k580270
64.2k580270
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered 29 mins ago
H.PWiz
9,68921249
9,68921249
add a comment |Â
add a comment |Â
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
@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
add a comment |Â
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
@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
add a comment |Â
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
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
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
add a comment |Â
@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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited 21 mins ago
answered 41 mins ago
Jonathan Allan
48.3k534159
48.3k534159
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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