Hot answers tagged

10

Jelly, 13 bytes ⁸JW;ṚZ+LƲƲ2¡¡ Try it online! A nilad function or full program that takes a number from stdin and returns the 1-based involute of size n × n. How it works ⁸JW;ṚZ+LƲƲ2¡¡ ⁸ Empty list ([]) 2¡¡ Take n from stdin and repeat 2n times starting from the above: Given previous matrix m, ṚZ+LƲ ...


8

MATL, 12 11 bytes UG1YL-GoQ&P Try it online! MATL (like MATLAB) has a spiral matrix which spirals clockwise from the center, so this performs the necessary flips and subtracts the matrix from N^2. Probably Luis Mendo knows how to golf this... Thanks to Luis Mendo for −1 byte. % Implicit input N U % Square G1YL % Push the N×N spiral matrix (S) &...


6

Python, 73 bytes (@ovs) f=lambda n,m=0,p=-1:n*[n]and((*range(m,n),),*zip(*f(2*n-m+p,n,~p)[::-1])) Attempt This Online! Old Python, 74 bytes (@DialFrost) f=lambda n,m=0,p=-1:n and((*range(m,n),),*zip(*f(2*n-m+p,n,~p)[::-1]))or() Attempt This Online! Old Python, 76 bytes f=lambda n,m=0,p=-1:n>m and((*range(m,n),),*zip(*f(2*n-m+p,n,~p)[::-1]))or() -15 by ...


5

Charcoal, 48 41 40 bytes F⊗N≔⁺⟦Eυλ⟧E∧υ⌊υ⁺LυE⮌υ§μλυEυ⪫Eι◧IλL⌈Eυ⌈ν Try it online! Link is to verbose version of code. Edit: Saved 4 bytes by porting @Bubbler's observation that you can vectorised add the current length of the array to all of its elements on each iteration, 3 bytes by special-casing an empty array to avoid having to perform the first step ...


5

BQN, 29 bytesSBCS {⍉⌽∘⍉∘∾´≍¨⌽(/«⥊2↕↕𝕩+1)⊔⌽↕𝕩⋆2} Run online! A port of Bubbler's Jelly answer comes in at 20 bytes: {(⊒˜∾⍉∘⌽+≠)⍟2⍟𝕩↕0‿0} Run online!


4

JavaScript (Node.js), 91 bytes f=(n,s=[[n*n-1]],[l]=t=s[0])=>l?f(n,[0,...t].map((_,i)=>s.map(r=>r[i-1]||--l).reverse())):s Try it online! A port of loopy walt's Python answer. Although I don't really understand what happening.


4

APL (Dyalog Unicode), 18 bytes (⍳∘≢⍪⍉∘⊖+≢)⍣2⍣⎕⊤⍨⍬ Try it online! Full program. TIO uses ⍵ instead of ⎕ for easier test case demonstration. For the core algorithm, refer to my Jelly answer. The only additional trick here is the ⊤⍨⍬, which is one of the shortest expressions that give a 0-by-0 matrix (0 0⍴0). This one in particular is handy because it doesn't ...


4

APL (Dyalog Unicode), 35 bytes {(⍵*2)-⌽{⍉⌽⍵,(⌈/,⍵)+⍳≢⍵}⍣(2×⍵-1)⍪0} Try it online!


3

JavaScript (ES7),  111  101 bytes Saved 9 bytes thanks to @tsh Returns a matrix. The results are 1-indexed. n=>[...Array(n)].map((x=-n/2,y,a)=>a.map(_=>n*n-(i=4*(++x*x>y*y?x:y)**2)+(x>y||-1)*(i**.5+x+y),y+=x)) Try it online!


2

Brachylog, 40 39 33 bytes -1 thanks to Unrelated String; -6 thanks to Fatalize ~b~k{{~c₃↺{Ṫ∋₁-₁ℕgj₃}ʰ↻c↰|}bk+}ᶠ⌉ Try it online! Brute force solution. V e r y   s l o w. For testing purposes, I suggest changing ℕ to ℕ₁, which helps significantly (but still not enough to solve most of the test cases in less than 60 seconds). Explanation This solution comes in ...


2

Jelly,  34 33  32 bytes I would have thought Jelly would be better at this, maybe a more mathematical approach will triumph? ŒMḢḢ +þ`¬ZṚ$ṛ¬Ç$¦€Ç¦‘ɼ$ḣÇȦƊ?ƬȦƇṂ A monadic Link accepting a positive integer that yields a list of lists of positive integers (top-left 1 option). Try it online! How? Start with an \$N\times N\$ table of zeros, then repeatedly either ...


1

05AB1E, 13 bytes ¯I·FāUøí¬g+Xš 1-based. Port of @Bubbler's Jelly answer, so make sure to upvote him as well! Try it online or verify all test cases. Explanation: ¯ # Start with an empty list [] I· # Push the input and double it F # Pop and loop that many times: ā # Push a list in the range [1,length] (without ...


1

JavaScript (Node.js), 108 bytes n=>[...Array(n)].map((_,Y,a)=>a.map(g=(y=Y,x,_,s=n)=>y?--s-x?s-y?x?s*4+g(y-1,x-1,_,s-1):4*s-y:3*s-x:s+y:x)); Try it online!


1

Python, 157 bytes f=lambda l:max([f((l[:max(i-1,0)]+[v-1]*(3-(0==i%(len(l)-1)))+l[i+2:])*((i>0 and v>l[i-1]+1)or(i<len(l)-1 and v>l[i+1]+1)))for i,v in enumerate(l)]+[sum(l)]) Try it online! Finds all the moves that would result in at least 1 element increasing value, then recursively tests those moves and returns the maximum value achieved or ...


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