Hot answers tagged

60

Operation Flashpoint scripting language, 182 bytes f={t=_this;c=count(t select 0);e=[0];i=0;while{i<c*2}do{e=e+[0];i=i+1};m=[e];i=0;while{i<count t}do{r=+e;j=0;while{j<c}do{r set[j*2+1,(t select i)select j];j=j+1};m=m+[r,e];i=i+1};m} Ungolfed: f= { // _this is the input matrix. Let's give it a shorter name to save bytes. t = _this; c = ...


42

vim, 126 80 77 76 /\v1011\_.{9}(1110\_.{9}){2}1111<cr>:exe'norm Go'.join(getpos('.'))<cr>xxdawhPXXd{ Expects input in the form 111001111110 110100100000 010001111101 100100100100 100101100111 111111000010 110111000001 100111100001 100111011111 111110011111 100001010111 110011000011 And outputs (with 1-based indices) as 4 5 / ...


30

Jelly, 20 17 16 bytes ṡ€4ḄZw€“¿ÇÇБĖUṀ Input is in form of a Boolean matrix, output is the 1-based index pair (Y, X). Try it online! or verify all test cases. How it works ṡ€4ḄZw€“¿ÇÇБĖUṀ Main link. Argument: M (2D array of Booleans) ṡ€4 Split each row into 9 chunks of length 4. Ḅ Convert these chunks from base 2 to ...


30

Wolfram Language (Mathematica), 15 bytes 2#-#~Erosion~1& Try it online! Or (39 bytes): MorphologicalPerimeter[#,Padding->1]+#& Try it online! What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}.


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 ...


28

J, 31 30 14 12 11 bytes [:;<@|.`</. Ych. Too big. Takes a matrix as input. Explanation J has an advantage here. There's a command called oblique (/.) which takes the oblique lines in turn and applies a verb to them. In this case I'm using a gerund to apply two verbs alternately: < (box) and <@|. (reverse and box). Then it's just a matter of ...


27

Jelly, 15 bytes LŒ!ðŒcIṠ;ị"Pð€S Try it online! How it works LŒ!ðŒcIṠ;ị"Pð€S input L length Œ! all_permutations ð ð€ for each permutation: Œc take all unordered pairs I calculate the difference between the two integers of each pair Ṡ ...


27

Python 3 + numpy, 45 bytes lambda\ a:a ma= """ b. " dT" " a " \ """ Try it online! Thanks to @EriktheOutgolfer pointing out a bug of the previous version Transposed: lambda\ a:a.T ma= """ b " d " " a " \ """ Try it online! The functions take a numpy matrix as input and output a numpy matrix. The solution does not rely on comments ...


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


22

JavaScript (ES6), 63 60 56 bytes s=>[(i=s.search(/1011.{9}(1110.{9}){2}1111/))%13,i/13|0] Takes input as a 155-character space-delimited string of 12 12-digit binary strings, returns zero-indexed values. Edit: Saved 3 bytes thanks to @JörgHülsermann. Saved 4 bytes thanks to @ETHproductions.


21

R, 43 34 bytes function(n)(8*n^3-3*n^2+4*n+3)%/%6 Try it online! The OEIS page lists the following formula for a(n): (16*n^3 - 6*n^2 + 8*n + 3 - 3*(-1)^n)/12 However, I skipped right over that to get to the PROG section where the following PARI code is found: floor((16*n^3 - 6*n^2 + 8*n + 3 - 3*(-1^n))/12)) Naturally, +3-3*(-1^n) is the same as +6 so ...


21

Jelly, 6 5 bytes pSÞỤs Try it online! How it works pSÞỤs Main link. Left argument: n. Right argument: k p Take the Cartesian product of [1, ..., n] and [1, ..., k], yielding [[1, 1], [1, 2], ..., [n, k-1], [n, k]]. SÞ Sort the pairs by their sums. Note that index sums are constant on antidiagonals. Ụ Grade up, sorting the ...


21

Octave, 57 bytes A=input('');V=A'/trace(A*A');for i=1:1e4V=2*V-V*A*V;end V Try it online! This is not particularly well golfed, but I wanted to advertise an approach that could be useful for other non-builtin answers. This uses the Hotelling-Bodewig scheme: $$ V_{i+1} = V_i\left(2I - AV_i\right)$$ Which iteratively computes the inverse of a non singular ...


20

J, 7 bytes <@|./. This is an unnamed a monadic verb which takes a matrix and returns a list of antidiagonals: input =. i.3 4 input 0 1 2 3 4 5 6 7 8 9 10 11 <@|./. input ┌─┬───┬─────┬─────┬────┬──┐ │0│4 1│8 5 2│9 6 3│10 7│11│ └─┴───┴─────┴─────┴────┴──┘ Test it here. Explanation /. is J's built-in to apply a function to each anti-...


20

Javascript, score 20 14, (258 176 non-newline characters) ///mmm()mu[=+r ///=.av=a,pr"= ///> p,>px=[ [ m=>//(yv()xp"] m. ////.(=+]+) map((////>y?u& v,y)=>v//r]r:& .map((u,x//[ur )=>r[p=m.//p) length-1-//], x+y]=r[p]//// ?r[p]+" "+u// :u),r=[])&&r and ///mmmv.)lx?: ///=.a,m=e+ru ///> pya>ny[) m=>//()prg]p, m. //(=(...


20

JavaScript (ES6), size 12 11 9 All versions return false for centrosymmetric or true for non-centrosymmetric. 1-dimensional array + length, size 9 (89 bytes) Takes input in currying syntax (length)(array), where array is 1-dimensional. w=>a=> // a.some // (v=>v- // a[--w])// ///////// //)]w--[a // -v>=v( // emos.a // >=a>=w Try it ...


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.


19

Grime, 20 bytes n`.\1./\3.\4/.\2.voO Try it online! A very literal implementation of the spec: n` make Grime count the number of subrectangles of the input that yield a match. .\1./\3.\4/.\2. defines the 3x3 square: .1. 3.4 .2. Where . can be any character. oO is an orientation modifier which allows this rectangle to appear in any rotation or ...


19

Canvas, 1 byte ╬ Try it here! Outputs as a multiline string


19

Octave, 24 bytes dec2base([0:3,5:8],3)-49 Try it online! I haven't seen this approach yet. Creates a list of integers [0, 1, 2, 3, 5, 6, 7, 8], and converts it to ternary, returning a character array: 00 01 02 10 12 20 21 22 Subtracting 49 (ASCII-value for 1) from all characters gives a numeric array: -1 -1 -1 0 -1 1 0 -1 0 1 1 -1 1 0 ...


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.


18

R, 40 38 bytes function(l)outer(m<-1:max(l),l,"<=")*m Try it online! Explanation: outer applies its third argument (the function) to all combinations of elements of its first two arguments, generating a matrix of TRUE and FALSE where each column has TRUE where 1:max(l) is less than or equal to the corresponding element of l, for the example where l=...


18

Jelly, score 2 ⁼Ṛ Ṛ⁼ Try it online! Takes input as a flattened square matrix (vector of size \$n^2\$).


18

MATL, 54 bytes th3LZ)t,3:q&XdpswP]w-lw/GtY*tXdsGXdsUw-IXy*2/+GtXds*-* Try it online! Just to keep it interesting, doesn't use the inbuilt matrix division or determinant functions to do it. Instead, computes the determinant using the Rule of Sarrus. And the adjugate (transposed cofactor matrix) using Cayley–Hamilton formula. $$ {\displaystyle \...


18

Python 2, 59 bytes n=input() c=~n%2 while c<n:print`n%10`*n+`c%10`*c;n-=1;c+=1 Try it online! Prints like: 55555 44441 33322 It looks kind-of redundant to update n-=1;c+=1 where sum n+c remains unchanged. I feel like there's a better way, but I haven't seen it so far. Bounty is up for grabs! 60 bytes n=input() b=a=n/2 while n-b:b+=1;print`a%10`*a+`...


18

Python 2, 37 bytes Outputs a flattened \$ n \times n \$ latin square. lambda n:((range(n)*-~n)[1:]*n)[::~n] Try it online!


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

Haskell, 21 bytes all$scanr1 max>>=(==) Try it online! Takes a list of columns, with empty spaces as 0.


16

Brachylog, 20 18 bytes ≠\≠,?¬{∋s₃=|∋ọtᵐ≠} Try it online! Explanation ≠ All rows are different \ Transpose ≠ All columns are different ,? Append the list of rows to the list of columns ¬{ } It is impossible that: ∋ ...


16

Snails, 17 16 bytes Ao \1.=\2o=\3b\4 Try it online! Explanation A makes Snails count the number of possible matching paths in the entire input. o sets the starting direction to any orthogonal direction (instead of just eastward). The pattern itself is on the second line: \1 Match a 1. . Match an arbitrary character (the one in the centre). =\2 ...


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