People who code: we want your input. Take the Survey

New answers tagged

0

Haskell, 128 bytes e=[]:e f(1:2:x)=2:2:f x f(x:y)=x:f y f z=z h a=any(>=[2])$iterate(foldl(zipWith$flip(:))e.map f)[2*x:y|x:y<-a]!!(4*length a^2+2) Try it online! Similarly to other answers, this "flood-fills" the input with 2s and checks if we reach the right edge. f propagates 2s leftward onto 1s in one row of the input: f [0,0,1,1,2] = [...


0

05AB1E, 3 bytes i[? 0Try it online! 1Try it online! i[? # full program i # if... # (implicit) hardcoded input... i # is 1... [ # forever... ? # output... # (implicit) hardcoded input # implicit output


0

APL (Dyalog Unicode), 24 bytes Uses the same method as Jonah's J answer. Full program that detects top-bottom connections. ⊃⌽⌈.×⍣≡⍨2>+/¨|∘.-⍨⍸1⍪⎕⍪1 Try it online! Footer transposes inputs.


2

J, 48 bytes _1{0{1([:+./ .*^:_~1>:|@-/~)@($j./@#:I.@,)@,~1,] Try it online! Takes a binary matrix, and checks for a top-to-bottom path. Hence, the input pre-processor transposes the test cases. the idea Add a row of ones to the top and bottom. Convert all ones to coordinates, encoded as complex numbers. Create an adjacency matrix for those points, with ...


0

I have adapted to my needs the Unicode shortcuts method by @bebe. The problem which many have faced with the original solution is due to the fact that some of the functions/properties belong to a class prototype while others to a class itself. These need to be handled separately. The minified code now takes 132 bytes: [Number,S=String,Array].map(r=>{f=(r=&...


1

Jelly, 28 bytes +þ2ŒRBU¤ẎQfðƬṪ ŒṪḷ/Ị$Ƈç$Ḣ€iL Try it online! -2 bytes thanks to caird coinheringaahing -2 bytes using transposed form Returns 0 for falsy and non-zero for truthy Uses the exact same breadth-first-fill helper link as my answer to "But, Is It Art?" +þ2ŒRBU¤ẎQfðƬṪ Helper Link; given a list of starting coordinates on the left and a ...


2

05AB1E, 42 36 bytes A slower but maybe golfable? version. After looking at Jonah's J answer I was able to remove 6 bytes. gÅ1¸.øεā*0K€¸<Nδš}€`DδαO2‹DvDδ*O}нθĀ Try it online! 05AB1E, 42 bytes A lot of this is similar to this recent answer of mine. Tests for a top-bottom connection. εā*0K€¸<Nδš}D€gU€`DδαO2‹DvDδ*O}Xθ.£øOXн£OĀ Try it online! Header ...


2

Red, 79 bytes func[x][p: 0 x: replace/all x"111111""1111110"forall x[prin p: 49 - x/1 xor p]] Try it online! Both input and output are strings of \$1\$s and \$0\$s. Output is done by printing. Despite the unwieldy string replacement, other ways to account for the extra zero bit tend to be even longer.


0

05AB1E, 4 bytes (n=1) .gΘ> Try it online! Explanation .gΘ> .g Push length of stack Θ Equals 1? > Increment


0

Vyxal, 4 bytes (n=1) ∴:∷› Try it Online! Same approach as my Jelly answer, but much more aesthetic. ∴:∷› ∴ Pop a, b; push max(a,b) : Duplicate ∷ Parity › Increment


1

A0A0, 165 bytes A0A0 A0C3G1G1G1G1G1A0 A0I1A6V0P0G6C6A0 A0A1G-3G-3G-3G-3G-3A0 G-3 A0A0 C3G1G1A0C3G1G1A0 A0A1G-3G-3A0 G-3 A0A0 C3G1G1A0C3G1G1A0 G-10G1G1A0G-10G1G1A0 A0A1G-3G-3A0 G-3 The program consists of three loops right after one another. The first loop does the following: I1A6V0P0G6C6 I1 ; take character input, store in V0 A6 ; ...


1

JavaScript (ES6),  103 101  94 bytes Expects a transposed binary matrix. Returns 0 or 1. f=(m,X,Y=-1)=>!m[Y+1]|m.some((r,y)=>r.some((v,x)=>v&&(X-x)**2+(Y-y)**2<2|!y&&--r[x]|f(m,x,y))) Try it online! Commented f = ( // f is a recursive function taking: m, // m[] = input matrix X, Y = -1 ...


2

R, 138 bytes function(m,k=nrow(m),r=m*0){r[,1]=m[,1] for(i in 1:k^2)r=r+m*(cbind(r[,-1],0)+cbind(0,r[,-k])+rbind(r[-1,],0)+rbind(0,r[-k,])) any(r[,k])} Try it online! At first, the matrix r contains only the 1s of the first column of the input m; all other entries are 0. At iteration i of the loop, entries which are attainable in at most i steps become non-...


4

MATLAB/Octave, 115* bytes function p=f(A) B=A*0;B(:,1)=A(:,1);for k=1:nnz(A) B=conv2(B,[0,1,0;1,1,1;0,1,0],'same').*A;end p=any(B(:,end));end Try it online! Takes binary array, outputs logical value. *for MATLAB it's possible to skim 3 bytes by replacing 'same' with 's'. Ungolfed: function path=f(A) B = A*0; % array of zeros with size of A B(...


2

K (ngn/k), 6 bytes -1+#2\ Try it online! Uses "The log base 2 of an integer is the same as the position of the highest bit set" as outlined here. #2\ get the position of the highest bit set -1+ subtract one If it's ok for an input of 0 to return 0 instead of -1, #1_2\ can be used to save a byte.


1

Scala, 87 bytes type R=Int=>Int type T=(=>Stream[R])=>R def>(f:Stream[T]):Stream[R]=f.map(_(>(f))) Try it in Scastie! This is very similar to Anders Kaseorg's answer, but it takes a lot more effort to avoid stack overflows and infinite recursion in Scala because it doesn't have lazy evaluation by default. //The result type, a complete ...


1

Python 3.8 (pre-release), 134 119 bytes lambda v,d:[i for i in product(*[range(8)]*d)if[1,2]==sorted(abs(x-y)for x,y in zip(i,v)if x-y)] from itertools import* Try it online! Generate all possible coordinates. Then, compute he list of the absolute difference between our vector and the coordinate. Remove all 0 in this list an verify that the remaining ...


3

Charcoal, 34 33 bytes FΦ↨⊕Lθ²κ≔⁺…⟦¹⟧ι⁺⊕ι⁺⁺υ⟦⁰⟧υυEυ◧§θκι Try it online! Link is to verbose version of code. Explanation: Based on @Bubbler's answer, so prints downwards indenting from left to right. FΦ↨⊕Lθ²κ Take the length of the input plus 1, convert to binary, and loop over the bits after the first. ≔⁺…⟦¹⟧ι⁺⊕ι⁺⁺υ⟦⁰⟧υυ Concatenate two copies of the ...


1

JavaScript (V8), 223 bytes f=(c,d=(s,x=0,y=0,u=s.substr.bind(s),l=s.length,h=l/2|0)=>l==1?{[x]:y}:l%2==0?{[x]:y,...d(u(1),x+1,y+1)}:{[x+h]:y,...d(u(0,h),x,++y),...d(u(++h),x+h,y)},r=d(c))=>Object.entries(r).map(k=>" ".repeat(k[1])+c[k[0]]).join("\n") Try it online! Maps the input source into a lookup of token index and ...


0

Japt -R, 40 36 bytes This'll need another pass once I'm properly caffeinated! 5Æ17î-³i+ÃíUs'#iS)ùG ®û3 i|ÃòG)cf ú| Try it 5Æ17î-³i+ÃíUs'#iS)ùG ®û3 i|ÃòG)cf ú| :Implicit input of integer U 5Æ :Map the range [0,5) 17î : Repeat the following to length 17 -³ ...


0

05AB1E, 27 26 bytes Takes the height and then block size on seperate lines. 30 bytes if the input format is strict. Lε'|²úRy²>*>©∍²и®'+×.ø}˜.c Try it online! L # push range [1 .. h] ε } # for y in [1 .. h]: '| '# push "|" ²úR # pad with w spaces in the ...


3

Ruby, 93 bytes ->a{w=*1..8;w.product(*[w]*~-a.size).select{|r|r.zip(a).map{|x,y|(x-y).abs}.sort-[0]==[1,2]}} Try it online! Quickly explained Generate all possible positions as vetors of N numbers between 1 and 8, then check the differences between each vector and the starting position, the sorted array of absolute values of the components must be [<...


0

MathGolf, 9 bytes ²╒kÉm■sk< Port of hyper-neutrino♦'s Python 2 answer, so will also output the first \$n\$ values (and will use the trick of only checking the range \$[1,n^2]\$). Try it online or verify some more test cases. Explanation: ² # Square the (implicit) first input `n` ╒ # Pop and push a list in the range [1,n²] k # ...


0

Vyxal o, 44 bytes (46 - 5%) (n£¹n-‹⁰½⌈*ð*:\+⁰›n›*›*+:→x,⁰(:₴\|:⁰꘍¥›*p,))←x Try it Online!


4

05AB1E, 12 bytes Takes N as first input and the current position as second input. 8LIãʒα0K{2LQ Try it online! 8L # push the range [1 .. 8] Iã # all N-tuples of integers in [1 .. 8] ʒ # only keep those for which ... α # ... the element-wise absolute difference to the current position 0K # ... without ...


0

MathGolf, 21 bytes f∩∩k(f/░▒φ░▒^╞╞mÅ▀£2= Try it online. Explanation: f # Get the n'th Fibonacci number using the (implicit) input-integer `n` ∩∩ # Convert it to a float (by changing `F(n)` to `1/1/F(n)`..) k # Push the input-integer again ( # Decrease it by 1 f # Get the n-1'th Fibonacci number as well / ...


1

Charcoal, 46 bytes FLθFLθF⊗¬⁼ικF²⊞υEθ⁺ν⁺×⁼ξι⊗∨λ±¹×⁼ξκ∨μ±¹IΦυ⁼ι﹪ι⁸ Try it online! Link is to verbose version of code. 0-indexed. Outputs using Charcoal's default array output of each element on its own line with separate results double-spaced from each other. Explanation: FLθ Loop over the possible N dimensions for the 2-step part of the knight's move. FLθ ...


1

05AB1E, 5 bytes вRĀ1k First input is base \$b\$, second input is \$n\$. Try it online or verify all test cases. Explanation: в # Convert the (implicit) input `n` to the (implicit) input-base `b` as list R # Reverse this list Ā # Truthify each value (0 remains 0; everything else becomes 1) 1k # And get the first (0-based) index of a 1 in ...


0

05AB1E, 12 bytes ∞ʒLDb1ö*y¢2@ Outputs the infinite sequence. Try it online. Explanation: ∞ # Push an infinite positive list: [1,2,3,...] ʒ # Filter each value `y` by: L # Pop and create a list in the range [1,`y`] D # Duplicate this list b # Convert each inner value to a binary-String 1ö # ...


3

Haskell, 14 bytes f l=map($f l)l Takes a two-element list of functions that take two-element lists, and returns a two-element list. Try it online!


4

JavaScript (V8), 114 bytes 0-indexed. Prints all valid vectors. v=>v.map((x,i)=>v.map((y,j)=>j>i&&(g=n=>n--&&g(n,(V=[...v],x-(V[i]=n&7))**2+(y-(V[j]=n>>3))**2-5||print(V)))(64))) Try it online! How? Let \$v\$ be the input vector of length \$N\$. For each pair \$(i,j),\:0\le i<j<N\$ and each value \$n\in[0\dots ...


4

Jelly, 13 bytes 8RṗLạṢ¹ƇؽƑʋƇ Try it online! Explanation 8RṗLạṢ¹ƇؽƑʋƇ Main monadic link 8R Range from 1 to 8 ṗ To the Cartesian power of L the length of the input Ƈ Filter by ʋ ( ạ Absolute difference with the input Ṣ Sort Ƈ Filter by ...


2

05AB1E, 26 bytes A port of Jonah's J answer. SõS¸λNÈi>0šë0N2÷₅>.ø]Igèú» Try it online! S # split input into list of characters õS¸ # push [[]] λ ] # starting with a(0)=[], calculate a(n) for n=0,1,... according to: NÈi # if n is even: > # each ...


1

Vyxal j, 47 bytes b:L16-(0p)4ẇƛƛ‛ #$i`| % `$%;ṅ\|+;\+:3-4*pw5ẋf$Y Try it Online! A big mess


1

Z80, 59 bytes Input value in dehl. Output in c, 0, 1, 2, 3, 4 for ldr, mvn, mov, movw, movs respectively. Trashes b and a. This is indeed rather silly. 0E 03 7C B7 20 01 0C 7B B2 C8 AF 4F 3C F5 7A 2F 57 7B 2F 5F 7C 2F 67 7D 2F 6F 06 20 37 CB 7A 20 01 3F CB 15 CB 14 CB 13 CB 12 7C B3 B2 20 03 F1 4F F5 01 E8 F1 D8 37 3C F5 18 D3 foo: ld c,3 //...


0

Stax, 16 bytes âìè╩Jù╠╕╜☻u$1∞;ⁿ Run and debug it just a simple ring translation. Takes the input directly through STDIN.


2

Vyxal ja, 22 bytes ƛ:`.,?*&$@!%`:ǓĿð4*$++ Try it Online! a flag makes Vyxal take all newline-separated inputs as a list. ƛ # Map... `.,?*&$@!%` # The characters :ǓĿ # Transliterate input by chars and chars shifted ?ð4*+ # Add four spaces to each line of the original ...


2

Vyxal j, 14 bytes ƛ×⁰\l=[↲¹]↳;øm Try it Online! 11 if 1/0 is allowed ƛ ; # Map to... × # Asterisk ⁰\l=[ ] # If left... ↲¹ # Left justify, then push size to right-just by ↳ # Right-justify øm # Palindromise


2

Hexagony, 11 bytes ,)</;(/@>;~ Try it online! Just some simple control flow on this one, could def be improved.


1

Vyxal, 67 bytes ȧ=[‹`\_/`*\|:„+p,‹3*\_*\|:„+p,|N3*⇩:\_*ðp,ð*\|:„+p,ȧ`\_/`*ḢṪ\|:„+p, Try it Online! Oof.


1

SM83, 8 bytes Input string pointer in de, output string pointer in hl 1A 13 23 B7 C8 23 18 F8 dbl: ld a,(de) // 1A read inc de // 13 and increment ld (hl+),a // 23 write and increment or a // B7 cheap test for zero ret z // C8 ...


1

Vyxal, 30 bytes (‛+on¬i:⁰-p,⁰(\|:⁰꘍p,))\o:⁰-p, Try it Online! ( ) # Length times... ‛+on¬i # If first iteration, o, else + : # Duplicate ⁰- # add (size) minuses p, # Add the o/+ and print ⁰( ) # ...


5

Jelly, 32 bytes ⁹ð:2ß‘},`j⁹ð’ß‘}⁹;ðḂ?Ị? Lç0⁶ẋż¹Y Try it online! -2 bytes thanks to caird coinheringaahing This uses essentially the same approach as Jonah, so upvote their answer as well. I came up with this solution independently, but Jonah's answer helped me consolidate a similar idea (to generate the indent array and convert that to indentation), and ...


0

Vyxal, 30 bytes (\|n*\++⁰n--øm,)\|*ð+øm¶+⁰d›-, Try it Online! ( ) # Input times... \|n* # (iteration number) |s \++ # With a plus appended ⁰n-- # And (input - iteration number) minuses øm, # Palindromed and outputted ...


10

APL (Dyalog Extended), 35 bytes ↑{⊃{0~⍨⍺-1-∊1⍵0⍵}/-⌽⍬⊂⍛,1↓⊤1+≢⍵}↑¨⊢ Try it online! Takes a string and puts each command on its own line top to bottom, indenting from left to right. The algorithm Given the length of the input n, the indentation pattern (the length of each line) looks like the following: f(1) = [1] f(2n + 2) = let prev = f(n) in [1] ++ (2+...


8

J, 67 bytes g=:(0,1+$:@<:)`([:(,0,])1+$:@<.@-:)`0:@.(=&1+2&|) f=:' '&,.#"1~1,.~g@# Try it online! This produces an upside-down left-to-right tree, which should be valid per the comments. For example, f '-1+!2' produces: - 1 + ! 2 the idea First notice the numerical pattern in the number of space indents: 1 -> 0 ...


1

Minecraft 1.16, 36 bytes Just something I thought of :D /tellraw @a {"text":"Hello, World!"} I can't find a try it online for these though


0

JSFuck (JScrewIt Firefox), 1053 bytes (+(!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[+[]]+(+!![])+(!![]+!![]+!![])+(+!![])+(!![]+!![]+!![]+!![]+!![])+(+!![])+(!![]+!![]+!![])+(!![]+!![])))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+!![]]+(!![]+[])[+[]]])[+!![]+[+[]]]+([]+[])[([]+[][(![]+[])[+!![]]+(!![]+[])[+[]]])[!![]+!![]+!![]]+(!![]+[][(![]+[])[+!![]]+(!![]+[])[...


0

Python 3 (63 bytes) def a(n):m=len(bin(n))-3;r=2**m;return n and a(n%r)+m*r/2+n%r+1 Here is my attempt 2, since dingledooper noted that my previous attempt was not polylog time complexity. Also, the golfiness can probably be improved by using walrus operator in python3.8 I used the formula found from "a curious PARI program on the OEIS page": a(n) ...


0

JScrewIt, 3522 bytes [][(![]+[])[+!![]]+(!![]+[])[+[]]][([]+[][(![]+[])[+!![]]+(!![]+[])[+[]]])[!![]+!![]+!![]]+(!![]+[][(![]+[])[+!![]]+(!![]+[])[+[]]])[+!![]+[+[]]]+([][[]]+[])[+!![]]+(![]+[])[!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+[][(![]+[])[+!![]]+(!![]+[])[+[]]])[!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!![]]+(!![]...


Top 50 recent answers are included