# Tag Info

30

MATL, 21 bytes 3XytPJ*-X*Xj~4&1ZIunq The input is a matrix with 1 for \ and j (imaginary unit) for /. Try it online! Or verify all test cases. With some extra code, you can see the different pieces in random colours. Or increase the resolution for a better looking result. Explanation Consider input [1,j; 1,1; j,1; j,j] as an example. This ...

29

MATL, 4 bytes BSXB Try it at MATL Online Explanation % Implicitly grab input as an array % STACK: [10, 17, 19, 23] B % Convert each element to binary where each decimal number results in a row % STACK: [0 1 0 1 0; % 1 0 0 0 1; % 1 0 0 1 1; % 1 0 1 1 1] S % Sort each column, placing all of the 1'...

28

Python 3, 124 bytes def f(n): while n&4369<n/n:n>>=1 while n&15<1:n>>=4 return'TJLZSIO'["rēȣc63ıGtĲȱᄑ@'̢̑@@@@Ȳq".index(chr(n))%7] Try it online! Expects an integer n representing a 4 × 4 binary matrix. Throws if no tetromino is found. Line 2 slides the shape to the right until a 1 is in the rightmost column. (4369 ...

28

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 shorter approaches, but this is what I could find for now. Please let me know if the algorithm is incorrect. Explanation Since there is exactly one line, there should be exactly one row/column ...

26

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): :t!= : take input n and a generate row array [1,2,...n] t duplicate ! zip = thread compare over the result

20

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 point of starting with B=A and then replacing B by A2B, occasionally clipping values larger than 1 to 1. The kth step of this process is equivalent up to the Clip to finding powers A2k+1, in which the (...

19

TI-BASIC, 2 bytes identity(Ans Fun fact: The shortest way to get a list {N,N} is dim(identity(N. Here's the shortest way without the builtin, in 8 bytes: randM(Ans,Ans)^0 randM( creates a random matrix with entries all integers between -9 and 9 inclusive (that sounds oddly specific because it is). We then take this matrix to the 0th power.

18

Julia, 9 3 bytes eye This is just a built-in function that accepts an integer n and returns an nxn Array{Float64,2} (i.e. a 2D array). Call it like eye(n). Note that submissions of this form are acceptable per this policy.

17

APL (Dyalog Unicode), 29 28 bytesSBCS -1 thanks to @Adám {≢∪∨.∧⍨⍣≡2>+/↑|∘.-⍨⍸⍵}¨⊂,~∘⊂ Try it online! ⊂,~∘⊂ the matrix and its negation { }¨ for each of them do ⍸⍵ list of pairs of coords of 1s +/↑|∘.-⍨ matrix of manhattan distances 2> neighbour matrix ∨.∧⍨⍣≡ transitive closure ≢∪ number of unique rows

17

J, 57 bytes ,&([:(0#@-.~~.@,)](*@[*[:>./((,-)#:i.3)|.!.0])^:_ i.@\$)-. Try it online! This is one of those where the idea is incredibly simple (and I think fun), but executing it had some mechanical lengthiness which masks the simplicity... eg, shifting the original matrix in all directions with 0 fill is the verbose ((,-)#:i.3) |.!.0. It's ...

16

APL, 5 bytes ∘.=⍨⍳ This is a monadic function train that accepts an integer on the right and returns the identity matrix. Try it here

16

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 of falsies (to be blanks) in the shape matrix All the ⎕s are supposed to be rectangles (they're not tofu). They symbolise a computer console which here means ...

16

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 idea is more interesting than the J mechanics here, I'll explain that. I could likely save bytes by translating one of the formulas into J, but that seemed less fun. the idea Generate all n! ...

15

MATL, 29 28 24 bytes z2Y6ttX*,GbZ+5Mz=z]yytvs Input is a binary matrix with 1 for '+' and 0 for '-'. Try it online! Or verify all test cases. Explanation Convolution is the key to success z % Implicit input. Number of nonzeros. This is the number of naive pluses 2Y6 % Push [0 1 0; 1 1 1; 0 1 0] (predefined literal): pattern of double plus ttX* ...

14

Octave, 10 4 bytes @eye Returns an anonymous function that takes a number n and returns the identity matrix.

14

Python 2, 42 bytes lambda n:zip(*[iter((+*n)*n)]*n)[:n] An anonymous function, produces output like [(1, 0, 0), (0, 1, 0), (0, 0, 1)], First, creates the list (+*n)*n, which for n=3 looks like [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0] Using the zip/iter trick zip(*[iter(_)]*n to make groups of n gives [(1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)] ...

14

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 bytes thanks to ngn. -5 bytes thanks to Mr. Xcoder. lambda h,j=''.join:j(' ZAQV;JWP;MBOS;YRKCGXDF;ILHUENT'[int('0'+i,27)%544%135%32]for i in j(chr(64|i&7|...

14

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 take the input as a string of 64 characters '0' or '1'. Using step slices, we cast four "lines of sight" from every queen we encounter. For example, when i = 10 and d = 7, marking the ...

13

Python 2, 275 271 264 249 bytes Saved four bytes by replacing -1 with H and removing one slicing operation ([:]). Saved seven bytes thanks to Halvard Hummel; removing yet another slicing operation ([:]), using multiple-target assignment to give a visited entry a value v not in "01" (S=S[1:];M[y][x]=H; -> S=M[y][x]=S[1:];) and switching from a ternary if/...

13

MATLAB/Octave, 115 109 98* bytes -6 bytes thanks to Luis Mendo -7 bytes thanks to Luis Mendo function p=f(A) B=A;B(:,2:end)=0;for k=1:nnz(A) B=conv2(B,mod(magic(3),2),'same').*A;end p=any(B); Try it online! (end keyword is in footer because it's not required when you define the function in a file) Takes binary array, outputs: truthy value as vector full of ...

13

Brachylog, 8 bytes {z|}≡ᵛ+1 Try it online! {z|} Either the rows or the columns ≡ᵛ are all the same list +1 which sums to 1.

12

R, 4 bytes diag When given a matrix, diag returns the diagonal of the matrix. However, when given an integer n, diag(n) returns the identity matrix. Try it online

12

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 pattern. The resulting pattern is the output. Explanation: 01234 Survive on any number of neighbors /234 Born on >2 neighbors V Only directly adjacent neighbors count Or if you ...

12

JavaScript (ES6),  148 146 143  142 bytes Saved 1 byte thanks to @ngn s=>[...s].map(c=>g((k=c.charCodeAt()/8)&8|k*8&7)&g(k&7|k/2&8),o=x='',g=n=>x=n?x*27+n:!(o+=' DZQGYWXNHJ.CSTIO.AFB.LPVE..KUMR'[x%854%89%35]))&&o Try it online!

11

16/9 20/11 22/12 28/15 30/16 32/17 34/18 36/19 (Java) This uses a number of ideas to reduce the search space and cost. View the revision history for more details on earlier versions of the code. It's clear that wlog we can consider only circulant matrices in which the first row is a Lyndon word: if the word is non-prime then it must have property X, and ...

11

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 every nth element from a list than splitting the list into chunks of length n and retrieving the head of each chunk. tN Get a list of all natural numbers except 1. (A) ...

11

Wolfram Language (Mathematica), 16+83=99 bytes Library import statement (16 bytes): <<Combinatorica` Actual function body (83 bytes): Length@HamiltonianCycle[MakeGraph[#~Position~1~Join~{1>0},##||Norm[#-#2]==1&],All]& Try it online! Note that the question just ask for the number of Hamiltonian path in the graph. However, (for some ...

11

MATL, 5 bytes JYaZy Try it online! Or verify all test cases. Explanation JYa % Implicit input. Unpad matrix. This removes outer zeros Zy % Size. Implicit display

11

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 the minimums (each row's minimum is the number of stages until it gets infected, so the number of stages needed is the maximum of the stages needed for each person). Conveniently, if all elements ...

11

Stencil ≢, 2 bytes ×v Try it online! ≢ tallies the number of necessary steps (including the initial state) until stability is reached. This command line argument isn't counted towards the byte count, as per Meta consensus. Each cell's next state is determined by: × the sign of v the sum of all values in its von Neumann neighbourhood (including itself)

Only top voted, non community-wiki answers of a minimum length are eligible