New answers tagged

3

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.


1

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, 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ö # ...


1

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


1

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


3

Haskell, 139 bytes ([]%) a%(x:y)|x=='_'=['a'..]!!foldl1((+).(2*))(take 5a):drop 5a%y|x>'@',x<'['=[x..]!!32:(a++[1])%y|x>'`',x<'{'=x:(a++[0])%y|1<2=x:a%y a%e=e Try it online!


2

Haskell, 85 bytes map("JK"!!).(0%) j%(1:1:1:1:1:1:y)=j:j:j:j:j:j:j%(0:y) j%(i:y)|k<-i^j*j^i=k:k%y j%e=e Try it online!


3

Red, 79 bytes func[x][p: s: 0 collect[foreach i x[s: any[all[s < 6 keep p = i s + 1]0]p: i]]] Try it online! Takes input as a block of \$1\$s and \$0\$s, outputs a block of booleans. Test suite on TIO uses two additional transformation functions that allow reading inputs from JK-strings as in task specification and convert output to numbers for nicer ...


1

C (gcc), 84 80 78 bytes -6 thanks to @ceilingcat x;c;a(char*p){for(c=0,x=1;*p;c>5&&a(L"10"+x))putchar(x^=*p<49),c=*p++/49*-~c;} Try it online! Takes input as the characters 0 and 1, outputs with bytes 0x01 for J and 0x00 for K. For output as actual J and K, Try it online! Decoder


3

<>^v, 161 bytes 1±n"0"o"1"i0q.h"J"j"K"kJtv v v=ICc®NHn)N v=N-?1¿H< >Q)7=v_qv >`! ~ vcOn(Nq0< v >Ktv T v< <>TJ=^Jtv ^ >C O=^ > ^ Explanation First line 1±n"0"o"1"i0q.h"J"j"K"kJtv 1 ...


2

K (ngn/k), 21 bytes 0=\.'(t,$0)/(t:6#$1)\ Try it online! Takes input as a string of "1"s and "0"s. Returns a list of 0s (for J) and 1s (for K). do a string replace of "111111" with "1111110" (t:6#$1) store "111111" in variable t (t,$0) append a "0" to the end of t, generating "1111110" ...


4

C (gcc), 79 76 bytes -3 thanks to @ceilingcat x;c;a(char*p){x=74;for(c=1;c*=*p==x,x=*p++;c=c>5?x=*p++,1:c+1)putchar(!!c);} Try it online! Takes input as J and K, outputs with bytes 0x00 and 0x01 for 0 and 1. For output as numeric 0 and 1, Try it online! for 78 bytes. Encoder


1

Python 3, 64 bytes f=lambda n:n and(2*f(n//2)+n//2+1if n%2else f(n/2)+f(n/2-1)+n/2) Try it online! from the recurrence relation: a(0) = 0, a(2n) = a(n)+a(n-1)+n, a(2n+1) = 2a(n)+n+1


2

Charcoal, 24 bytes ≔⪫⪪S×1⁶⁺×1⁶0θ⭆θ§JK№…θ⊕κ0 Try it online! Link is to verbose version of code. Explanation: ≔⪫⪪S×1⁶⁺×1⁶0θ Perform bit-stuffing. ⭆θ§JK№…θ⊕κ0 For each prefix of the string count the number of 0s and output K if it is odd and J if it is even.


5

Husk, 11 bytes G=Jḋ126xḋ63 Try it online! Input as array of 0s and 1s, output as array of 1s (for state J) and 0s (for state K): TIO header converts to Js and Ks. x # split input on ḋ63 # binary digits of 63 = [1,1,1,1,1,1] J # then join back together using ḋ126 # binary digits of 126 = [1,1,1,1,1,1,0] G= # and ...


4

Jelly, 12 bytes 7Ṭ¬©Ṗœṣ@j®⁼\ A monadic Link that accepts a list of integers (from \$[0,1]\$) and yields a list of integers (from \$[0,1]\$) where \$0\$ represents \$K\$. Try it online! Or see the test-suite. How? 7Ṭ¬©Ṗœṣ@j®⁼\ - Link: list of integers in [0..1], A 7 - seven Ṭ - untruth -> [0,0,0,0,0,0,1] ¬ - logical NOT -&...


3

Vyxal, 16 bytes \16*:0+V1$(nɽ߬… Try it Online! \16* # Six ones :0+ # Duplicate and append a 0 V # Replace in input 1$ # Starting with a one ( # Iterating through the string ߬ # Flip if... nɽ # It's a 1 … # Print without popping Returns 1 for J ...


3

R, 59 bytes function(x)cumsum(49-utf8ToInt(gsub('(1{6})','\\10',x)))%%2 Try it online! Regex-based bit-stuffing stolen from inspired by pajonk's R answer to the paired usb-decoding challenge. The state-switching is achieved here by determining whether the cumulative sum is odd or even.


3

convey, 31 bytes Outputs KJ as 01. v|6 0"("=0 +*6!<} ""~,=" { 5 1< Try it online! In the top left is a counter that gets incremented by the input 1s + and reset whenever it reaches 6 |6 or the input has a 0 *. If it reaches 6, it also injects a 0 into the stream (the ("=0 6!< part). Because the input stream would be ...


4

JavaScript (ES6), 53 bytes Expects a binary string. Returns an array of 0's and 1's. s=>[...s.replace(/1{6}/g,'$&0')].map(q=c=>q^=c^!!++q) Try it online! Commented This is a rather straightforward implementation in two steps. s => // s = input string // STEP 1 - Bit stuffing // [...s.replace( // replace ...


3

Jelly, 14 bytes “?~‘Bœṣjƭƒ^¬¥\ Try it online! -2 bytes thanks to Nick Kennedy “?~‘Bœṣjƭƒ^¬¥\ Main Link; accepts a list of [0, 1] on the left “?~‘ 63, 126 B To binary ([1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 0]) ƒ Reduce starting with the input: ƭ Tie; cycle œṣ The first time, split on sublist ...


2

Retina 0.8.2, 33 32 bytes 1{6} $&0 {+rT`d`JK`K. 1T`d`KJ`\d Try it online! Link includes test cases. Explanation: 1{6} $&0 Perform bit-stuffing. {` Make as many replacements as possible. +rT`d`JK`K. Change 0 after a K to J and 1 after a K to K. Change starting with the character after the last K (and working back, not that it matters), and keep ...


1

Python 3, 81 bytes def f(A): c=0 while A>1: a=len(bin(A))-3;A=A%(2**a);c+=2**a*a/2+A return c Try it online! For exponentials of 2 -1 every bit is flipped 50% of the time so the total sum of bits is n*(# of bits)/2 this gets done recursively. For every further recursion we also need to add up the first bits that get progessively ignored.


2

05AB1E, 13 bytes 0ìø€ËJƵPbD¨.: Try it online! Uses 0 for J and 1 for K. 0ìø€ËJƵPbD¨.: # full program .: # replace all instances of... ƵP # 126... b # in binary... .: # in... J # joined... Ë # are all digits of... € # each element in... # implicit input... ...


1

Jelly, 14 bytes ŻI¬“~?‘B¤œṣjƭƒ Try it online! A monadic link taking the argument as a vector of 0 (=J) and 1 (=K) and returning a vector of 0 and 1.


2

K (ngn/k), 23 bytes *|+(126=2/)_7'(&6),0=': Try it online! Takes J as 0, and K as 1. Returns a list of 0s and 1s. 0=': run an equals-each-prior, seeded with 0 on the (implicit) input, handling the non-return-to-zero part 7'(&6), prepend 6 0s to the input, then slice into length-7 sliding windows. the first 6 slices will contain some dummy 0s (126=2/...


7

Husk, 11 bytes σḋ126ḋ63Ẋ=Θ Try it online! Takes an array of bits(0=J, 1=K), outputs an array of bits(formatted as string for convenience) Explanation σḋ126ḋ63Ẋ=Θ Θ Prepend 0 Ẋ= map overlapping pairs by equality σḋ126ḋ63 replace binary digits of 126 with binary digits of 63 (stolen from Luis Mendo)


2

Stax, 16 bytes Å3≈àE∩◄σ←π<δô╝ìà Run and debug it takes in a string/array of bytes 0x00 for J and 0x01 for K. Outputs a string, same as the given testcases. Sadly, this 14 byte program doesn't work since stax auto-replaces null bytes with spaces.


4

Python 3, 106 104 bytes def f(A): A+=1;a=0 for i in range(32): x=2<<i;d,r=A//x,A%x;a+=d*x/2 if 2*r>x:a+=r-x/2 return a Try It Online!! This is designed for 32-bit integers, here I am using float to save some bytes from "//" operator, I hope you won't mind \$x.0\$ instead of \$x\$, further bytes can be reduced if I allow ...


2

Charcoal, 19 bytes ⪫⪪⭆θ⁼ι§⁺Jθκ⁺×1⁶0×1⁶ Try it online! Link is to verbose version of code. Explanation: θ Input string ⭆ Map over characters and join J Literal string `J` ⁺ Concatenated with θ Input string § Indexed by κ Current index ...


3

Python 2, 77 bytes lambda s:''.join((`+(x==y)`for x,y in zip('J'+s,s))).replace('1'*6+'0','1'*6) Try it online! -2 bytes thanks to @ovs


11

convey, 31 25 bytes Takes JK as 01. >="*>0 ",v+<"(6 {0>">>!`} Try it online! Compares = the string with itself with a J prepended ,0. The middle loop just adds the comparisons to a counter (starting with 0) that gets reset on a 0. Only take ! elements where the previous counter was less than 6 (6.


1

Retina 0.8.2, 31 bytes (.)(?<=(\1.|^J)?) $#2 1{6}0 6$* Try it online! Link includes test cases. Explanation: (.)(?<=(\1.|^J)?) For each letter, look behind for a possible duplicate or a leading J. $#2 If it was then replace it with a 1 otherwise replace it with a 0. 1{6}0 6$* Remove the 0 that appears after a run of six 1s.


2

Charcoal, 28 bytes NθIΣE⮌↨θ²⁺×÷θX²⊕κX²κ×ι⊕﹪θX²κ Try it online! Explanation: For a given bit position k, the sum of the bits at position k from 1 to n depends on whether the kth bit of n is 0 or 1: If the kth bit of n is 0, then the bits at position k are of the form 0...1...0...1...0...1...0..0. Each run of 2ᵏ⁺¹ integers contributes 2ᵏ bits. If the kth bit ...


3

PHP -F, 96 bytes for($s=$r=$argn;$s[++$i]!='';)$r[$i]=+!abs($s[$i]-$s[$i-1]);echo str_replace(1111110,111111,$r); Try it online! Well, I.. tried.. to make it short.. hum! takes a string of 0 (J) and 1 (K) as input


3

Vyxal, 16 bytes 1p-Ṫv¬ṅ\16*:0+$V Try it Online! 1p # Prepend a 1 - # Differences - truthy if unchanged, falsy if changed Ṫ # Get rid of last v¬ # Vectorised not ṅ # Joined \16* # Six ones : # Duplicate 0+ # Append a 0 to the first $...


1

Jelly, 14 bytes Ż⁼Ɲ0ȯ1בʋ7Ƒ?ɼƇ Try it online! Takes J and K as 0 and 1.


6

MATL, 15 14 bytes 0ihd~126B63BZt Input is an array with 0 for 'J' and 1 for 'K'. Try it online! Or verify all test cases. Explanation 0 % Push 0 i % Input: string h % Concatenate d % Consecutive differences ~ % Negate: converts nonzero to 0, and zero to 1. Gives a numeric vector (*) 126B % Push 126, convert to binary. Gives [...


4

R, 83 76 62 bytes function(x)gsub('(1{6})0','\\1',Reduce(paste0,+!diff(c(0,x)))) Try it online! Takes input as 0(J) and 1(K). Outputs string of 1s and 0s. Change of approach after looking at @Luis Mendo's answer. function(x) # function taking x as input gsub( # replace all occurences of '(1{6})0', # 6 ones and a zero '\\1', # with ...


4

JavaScript (ES6), 54 bytes s=>s.replace(/./g,a=>s^(s=a>"J")?i=i>5?'':0:++i/i,i=0) Try it online! Take input as a string of "J" / "K". Output a string with "0" and "1". And f=J=>J.replace(/./g,a=>J^(J=a>f)?i=i>5?'':0:++i/i,i=0) is 54 bytes too. JavaScript (ES6), 44 bytes s=>s....


3

J, 26 bytes Takes in JK as 0 1. t#~0=_6|.(6$1)E.t=:2=/\0,] Try it online! t#~0=_6|.(6$1)E.t=:2=/\0,] 0,] prepend a J 2=/\ pair-wise equal t=: store as t (6$1)E. bitmask of places where 6 ones starts _6|. shifted by 6 bits 0= ...


1

JavaScript (Node.js), 61 bytes n=>n.replace(/./g,(e,i)=>1-e^n[i-1]).replace(/1{6}0/g,111111) Try it online! I have tried to make sure it outputs numbers. -10 bytes by pxeger


6

JavaScript (ES6), 51 bytes f=(n,i=0,p=1<<i)=>n<p?0:f(n,i+1)+(n&p&&n%p+1+p*i/2) Try it online! Let we denote $$ n = \left(\overline{b_mb_{m-1}\dots b_2b_1b_0}\right)_2 $$ Then $$ f\left(n\right)=\sum_{i=0}^{m} b_i \cdot \left(\left(n \bmod 2^i\right) + 1 + 2^{i-1}\cdot i\right) $$ This function loops from \$0\$ to \$m\$ with constant ...


13

JavaScript (ES6),  57 54 53  52 bytes n=>(i=0,g=b=>b--&&(n>>b&1&&b+i+i++<<b)+g(b))(32)/2+i Try it online! This is designed for 32-bit integers and always performs 32 iterations, no matter the input value. The following version performs \$\lfloor log_2(n)\rfloor+1\$ iterations instead, but this is really pointless since ...


13

K (ngn/k), 45 32 bytes {+/b*1+(q!'x)+-2!q*<q:2/=#b:2\x} Try it online! Golfing ideas thanks to @ngn and @coltim The OEIS page contains a curious PARI program: a(n)=if(n==0,0,m=logint(n,2);r=n%2^m;m*2^(m-1)+r+1+a(r)); which has a recursion depth of \$\mathcal{O}(\log n)\$ and each recursive call involves constant number of arithmetic operations (and ...


0

MMIX, 24 bytes (6 instrs) 00000000: 36ff0000 c8ffff00 3bffff01 c80000ff 6”¡¡Ṁ””¡;””¢Ṁ¡¡” 00000010: 73000001 f8010000 s¡¡¢ẏ¢¡¡ Disassembled dragon NEGU $255,0,$0 // t = -n AND $255,$255,$0 // t &= n (gets last 1 bit) SLU $255,$255,1 // t <<= 1 AND $0,$0,$255 // n &= t ZSZ $0,...


0

Vyxal, 8 bytes bṘ:1ḟ›i¬ Try it Online! Straightforward use of the formula b # To bitlist Ṙ # Reverse : # Duplicate 1ḟ # First truthy value › # Incremented i # Indexed ¬ # Flipped


2

Python 3.8 (pre-release), 62 bytes exec(a:="print(''.join(f'{i:08b}'for i in b'exec(a:=%r)'%a))") Try it online! How it work : exec(a:="print('exec(a:=%r)'%a)") is a quine f'{i:08b}' converts i into its 8-digits binary representation b'example_string' is a binary sting. When we iterate over it, it convert the characters by their ...


Top 50 recent answers are included