Whose neighbours are hostile?
Clash 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)]
code-golf array-manipulation arithmetic integer matrix
add a comment |Â
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)]
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
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
add a comment |Â
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)]
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!
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
code-golf array-manipulation arithmetic integer matrix
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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)
add a comment |Â
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]
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
add a comment |Â
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
@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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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 Sep 16 at 18:15
H.PWiz
9,75921249
9,75921249
add a comment |Â
add a comment |Â
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)
add a comment |Â
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)
add a comment |Â
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)
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 Sep 16 at 16:54
answered Sep 16 at 12:40
Arnauld
65.5k583277
65.5k583277
add a comment |Â
add a comment |Â
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]
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
add a comment |Â
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]
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
add a comment |Â
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]
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]
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
add a comment |Â
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
add a comment |Â
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
@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
add a comment |Â
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
@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
add a comment |Â
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
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 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
add a comment |Â
@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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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 Sep 16 at 18:23
answered Sep 16 at 18:03
Jonathan Allan
48.6k534160
48.6k534160
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Sep 17 at 3:31
answered Sep 16 at 19:59
Chas Brown
4,3111319
4,3111319
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Sep 18 at 7:57
Delfad0r
768213
768213
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
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