30
votes
Slash the matrix
MATL, 21 bytes
3XytPJ*-X*Xj~4&1ZIunq
The input is a matrix with 1 for \ and ...
- 102k
29
votes
29
votes
Is there a single straight line in the matrix?
Python 2, 49 bytes
Takes as input a 2D binary matrix \$ a \$, and its size \$ n \$.
lambda a,n:sorted(map(sum,a+zip(*a)))[-2:]==[1,n]
Try it online!
There may be ...
- 20.3k
28
votes
26
votes
Construct the Identity Matrix
MATL, 2 bytes
Xy
A translation of my Octave answer.
Try it online.
A 4 byte version with no built-ins (thanks to Luis Mendo):
...
- 11.1k
20
votes
Is it bipartite?
Wolfram Language (Mathematica), 26 25 bytes
Tr[#//.x_:>#.#.Clip@x]<1&
Try it online!
How it works
Given an adjacency matrix A, we find the fixed ...
- 5,197
19
votes
Construct the Identity Matrix
TI-BASIC, 2 bytes
identity(Ans
Fun fact: The shortest way to get a list {N,N} is ...
- 21.3k
18
votes
Construct the Identity Matrix
Julia, 9 3 bytes
eye
This is just a built-in function that accepts an integer n and returns an ...
- 24.4k
18
votes
To find islands of 1 and 0 in matrix
J, 57 bytes
,&([:(0#@-.~~.@,)](*@[*[:>./((,-)#:i.3)|.!.0])^:_ i.@$)-.
Try it online!
This is one of those where the idea is incredibly simple (and I ...
- 30.9k
17
votes
Construct the Identity Matrix
APL, 5 bytes
∘.=⍨⍳
This is a monadic function train that accepts an integer on the right and returns the identity matrix.
Try it here
- 24.4k
17
votes
To find islands of 1 and 0 in matrix
APL (Dyalog Unicode), 29 28 bytesSBCS
-1 thanks to @Adám
{≢∪∨.∧⍨⍣≡2>+/↑|∘.-⍨⍸⍵}¨⊂,~∘⊂
Try it online!
⊂,~∘⊂ the matrix ...
- 15.1k
16
votes
ASCII texturing
APL (Dyalog Unicode), 20 12 bytesSBCS
-6 bytes through dzaima
Full program. Prompts stdin for:
texture as a list of strings
number of rows in shape matrix
number of columns in shape matrix
indices ...
- 29.2k
16
votes
Just Enough Ones
J, 41 bytes
1#.*:=1#.2>[:>@~.@,[:+&,&.>/~i.@!<@A.=@i.
Try it online!
A different take on brute force, which handles up to n=6 on TIO.
Since the ...
- 30.9k
16
votes
Count my Pluses
MATL, 29 28 24 bytes
z2Y6ttX*,GbZ+5Mz=z]yytvs
Input is a binary matrix with 1 for '+' and <...
- 102k
15
votes
Construct the Identity Matrix
Python 2, 42 bytes
lambda n:zip(*[iter(([1]+[0]*n)*n)]*n)[:n]
An anonymous function, produces output like ...
- 141k
15
votes
14
votes
Construct the Identity Matrix
Octave, 10 4 bytes
@eye
Returns an anonymous function that takes a number n and returns the identity matrix.
- 11.1k
14
votes
Decode Faux Braille
Python 3, 181 179 171 167 161 159 bytes
Input by UTF-16 little-endian bytes without BOM. First decompose into columns using bit shifts, split by empty column, then hash them into a lookup table.
-2 ...
- 2,421
14
votes
Mutually Attacking Queens
Python 2, 105 bytes
lambda b:sum(b[i+d::d][:(8,7-i%8,i%8)[d%8%5]].find('1')*int(c)>0for i,c in enumerate(b)for d in[1,7,8,9])
Try it online!
Explanation
We ...
- 66.8k
13
votes
Accepted
The last stage of contamination
Since this is basically talking about a cellular automaton I give you..
Golly Quicklife rule, 10 bytes
01234/234V
Input the rule, paste the grid into Golly, run ...
- 2,758
13
votes
Finding Snakes in a Matrix
Python 2, 275 271 264 249 bytes
Saved four bytes by replacing -1 with H and removing one slicing operation (...
- 7,311
13
votes
Is there a left-right connection?
MATLAB/Octave, 115 109 98* bytes
-6 bytes thanks to Luis Mendo
-7 bytes thanks to Luis Mendo
...
- 868
13
votes
12
votes
Construct the Identity Matrix
R, 4 bytes
diag
When given a matrix, diag returns the diagonal of the matrix. However, when given an integer ...
- 24.4k
12
votes
11
votes
The Binary Square Diagonal Sequence
Husk, 15 14 bytes
zȯḋm←CtNCİ□ṁḋN
Try it online!
Continually prints the results as an infinite list.
Explanation
I wonder whether there's a better way to get ...
- 195k
11
votes
Accepted
Where is that snake going?
Wolfram Language (Mathematica), 16+83=99 bytes
Library import statement (16 bytes):
<<Combinatorica`
Actual function body (83 bytes):
...
- 17k
11
votes
Trim that distracting background off!
MATL, 5 bytes
JYaZy
Try it online! Or verify all test cases.
Explanation
...
- 102k
11
votes
Calculate COVID spread
Jelly, 10 bytes
ŒJạ€ŒṪ§Ṃ€Ṁ
Try it online!
-2 bytes thanks to Sisyphus
Calculate the Manhattan differences from all 0s to all 1s, and the answer is the maximum of ...
- 40.6k
11
votes
Calculate COVID spread
Wolfram Language (Mathematica), 90 78 bytes
f=Length@FixedPointList[ListConvolve[CrossMatrix@1,#,{2,2},0,Times,Max]&,#]-2&
Try it online!
-12 bytes, ...
- 261
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
binary-matrix × 66code-golf × 60
matrix × 12
grid × 11
array × 11
decision-problem × 10
ascii-art × 6
code-challenge × 5
math × 4
graph-theory × 4
string × 3
number × 3
game × 3
binary × 3
restricted-time × 3
sequence × 2
geometry × 2
combinatorics × 2
unicode × 2
linear-algebra × 2
open-ended-function × 2
kolmogorov-complexity × 1
integer × 1
fastest-code × 1
base-conversion × 1