Hot answers tagged

13

Gema, 17 characters [#]?=@repeat{?;#} Sample run: bash-4.4$ gema '[#]?=@repeat{?;#}' <<< '[three[two[one]1]2]3' threetwoonetwoonethreetwoonetwoonethreetwoonetwoone


12

Jelly, (17 bytes + 18 bytes) × length 6 = 210 points b36µỤỤ+×3µŒ¿b3U+Ṣ Ṣ:3_J Ṣ%3Uḅ3œ?Çḅ36 Try it online! (or with extra debug info) Having had a go at solving this challenge aiming at the stated victory condition, I thought it'd be interesting to go for a hypothetical alternative victory condition: code-golf given a minimum possible maximum length for ...


10

JavaScript (ES6), 26 bytes Takes input in little Endian. n=>'0958634172'[n*3%77%10] Try it online!


9

Jelly, (4 3 bytes + 6 5 bytes) × length 8 = 80 64 points b⁴ÄṢŻIḅ⁴ Try it online! Jelly, (2 1 byte + 4 3 bytes) × length 10 = 60 40 points ÄṢŻI Try it online! Explanation Solution 1 This is using a different algorithm from most of the other answers. We start by encoding the value in hexadecimal (b⁴), as with the other answers, then take a cumulative ...


8

Retina, 24 23 22 bytes +`\[([^][]*)](.) $2*$1 Try it online! This is practically a builtin in Retina 1. Edit: Saved 1 byte thanks to @Kobi. 47 45 bytes in Retina 0.8.2: ]. ]$&$*¶ {+`\[([^][]*)]¶ $1[$1] \[([^][]*)] Try it online!


8

Python 3, 18 bytes b'~0my3[_p{'.find Try it online! Uses little-endian inputs. Contains a raw \x7F byte. Python 2, 27 bytes map(ord,'~0my3[_p{').index Try it online!


7

Haskell, 101 96 bytes fst.(""%) infix 4% s%']':d:r=(['1'..d]>>s,r) s%'[':r|(t,q)<-""%r=s++t%q s%x:r=s++[x]%r s%e=(s,e) Try it online! Instead of using regular expression like most of the other answers, this implements a recursive parser. -5 bytes thanks to BMO!


7

Perl 5, 34 33 29 + 1 (-p) = 30 bytes s/.([^[]*?)](.)/$1x$2/e&&redo Try it online! Cut it down with some help from @Shaggy and @TonHospel.


7

Shakespeare Programming Language, 10 * (264 + 494) = 8650 7910 7580 Encoder: 264 bytes ,.Ajax,.Ford,.Act I:.Scene I:.[Exeunt][Enter Ajax and Ford]Ajax:Open mind.Be you nicer zero?You be the sum ofyou the product ofthe sum ofI a big big pig the sum ofa big big big big cat a big big pig.If soSpeak thy.Ford:You are the sum ofyou a cat.If soLet usAct I. Try ...


7

Wolfram Language (Mathematica), 41 bytes 9[,6,0,8,2,3,1,7,5,4][[#~Mod~41~Mod~11]]& Try it online! Uses the little-endian column of integers as input. Ignore the syntax warning. For an input X, we first take X mod 41 and then take the result mod 11. The results are distinct mod 11, so we can extract them from a table. For example, 126 mod 41 mod 11 is ...


6

Pyth, 98 97 95 93 90 83 80 bytes The code contains unprintable characters, so here is a reversible xxd hexdump: 00000000: 753f 7133 4a69 4832 5047 2b47 3f3c 334a u?q3JiH2PG+G?<3J 00000010: 4040 6332 2e22 275a 75ae 5751 fb4e 3cd7 @@c2."'Zu.WQ.N<. 00000020: 02ce 8719 aac1 e0e0 fe1f 09e5 85bc a767 ...............g 00000030: 8e0c 1f47 508a cad1 1acb ...


5

Python 3, 8 * (45 + 38) = 664 Encoder (45 bytes): lambda n:[16*i+(n>>4*i)%16 for i in range(8)] Decoder (38 bytes): lambda l:sum(x%16<<x//16*4 for x in l) Try it online!


5

Python 2.7, 31 * (52 + 37) = 2759 Encoder (69 52 bytes): lambda n:[chr(i)if n&(1<<i)else""for i in range(32)] Decoder (41 37 bytes): lambda s:sum([1<<(ord(c))for c in s]) Stores the all the non-zero bits in the input number as ascii values. The value of the ascii character stores the position of the set bit. For example the value 'a' ...


5

JavaScript (ES6), 160 158 153 bytes let f = s=>s.replace(/.{5}/g,s=>(n='0b'+s-1)<2?m-n?(m^=1,''):' ':"? !YSBREXGMIWFNA-JKUTCQ/ZHLOVDP? ?!3 8-2 7) ? 1.6(4 9/ : =5'0+"[n+m*32],m=0).replace(/.!/g,'') console.log(f("001101000010100111101110010101")); console.log(f("11010010001001100011110111101111100")); console.log(f("...


5

Batch, 306 304 bytes @echo off set/pc= set r= set d=! !!YSBREXGMIWFNA-JKUTCQ/ZHLOVDP!! !3!8-2!7)!?!!1.6(4!9/!:!=5'0+ set s=2 :l set/an=(s^&32)+0%c:~,2%%%6*8+0x%c:~2,3%%%14 set c=%c:~5% if %n%==%s% set/as^^=35&goto l call set r=%%r%%%%d:~%n%,1%% if %r:~-1%==! set r=%r:~,-2%&goto l if not "%c%"=="" goto l echo %r% Takes input on STDIN. Since ...


5

Japt v2, 21 20 19 bytes Saved 2 bytes thanks to @Shaggy e/.([^[]*?)](./@YpZ Test it online! e is recursive replace, which makes one replacement at a time until there are no more matches. In this case, matches of the regex /\[([^[]*?)](\d)/g are replaced with with <inner text> repeated <digit> times until there are no more matches. According to ...


5

Stax, score 8 × (10+9) = 152 Encoder, 10 bytes Ç·MÉJ'♀τ│½ Run and debug it 16|E{i16*+m Full program, implicit input 16|E Get hexadecimal digits { m Map: i16*+ Add 16 * loop index Implicit output as string The encoder outputs the string in an increasing order. Decoder, 9 bytes üL∟n╫k∞‼9 Run and debug it o{16%...


5

05AB1E, 8 maximum length * (8 + 7) bytes = 120 Encoder (8) hεHN16*+ Try it online! Decoder (7) {16%hJH Try it online! Uses the same technique as wastl and Jonathan.


4

Python 3, 110 93 92 bytes import re f=lambda s:f(re.sub(r'\[([^][]+)\](.)',lambda m:m[1]*int(m[2]),s))if'['in s else s Try it online! -17 bytes thanks to pizzapants184 -1 byte thanks to Kevin Cruijssen


4

JavaScript, 71 67 66 bytes I had a 54 byte solution but it got screwed over by the second test case! :( f=s=>s!=(x=s.replace(/.([^[]*?)](.)/,(_,y,z)=>y.repeat(z)))?f(x):x Test Cases f=s=>s!=(x=s.replace(/.([^[]*?)](.)/,(_,y,z)=>y.repeat(z)))?f(x):x o.innerText=`[Foo[Bar]3]2 [one]1[two]2[three]3 [three[two[one]1]2]3 [!@#[$%^[&*(]2]...


4

Scala, 173 bytes l.foreach{x=>def r(c:String):String={val t="""\[([^\[\]]*)\](.)""".r.unanchored;c match{case t(g,h)=>r(c.replaceAllLiterally(s"[$g]$h",g*h.toInt));case _=>c}};println(r(x))} Try it online! Expanded: l.foreach { x => def remove(current: String): String = { val test ="""\[([^\[\]]*)\](.)""".r.unanchored current match {...


4

JavaScript (ES6), 8 * (40 + 32) = 576 The encoder outputs an array of \$0\$ to \$8\$ integers. The decoder takes the same format as input. Encoder (40 bytes) E=(n,k=0)=>n?[k|n&15,...E(n>>4,k+16)]:[] Decoder (32 bytes) s=>s.map(c=>s|=c%16<<(c/4&~3))|s Demo Try it online! How? The input is divided into 8 blocks of 4 bits ...


4

Jelly, (8 + 9) bytes * 8 max-length = 136 b⁴+J’Ɗ⁴¡ Encoder (footer formats the list as Python would for clarity) Ṣ_J‘Ɗ⁴¡ḅ⁴ Decoder It's theoretically possible to have a max-length of six, can that be done in 22 bytes or fewer? It's impossible with a max-length of five since \$\sum_{i=0}^{i=5}\binom{127+i}{127}=321402081< 2^{31}-1\$ How? Since \$2^{...


4

Shakespeare Programming Language, 31 * (472 + 383 379 344) = 26505 26381 25296 Previous score: 16909322 * (246 + 217) = 7829016086 This is still very high, but it's the lowest I can think of right about now. Encoder: ,.Ajax,.Ford,.Act I:.Scene I:.[Enter Ajax and Ford]Ajax:Remember a pig.Ford:Listen tothy.Scene V:.Ajax:Remember the remainder of the ...


4

Jelly, 12 bytes “0my3[_p¶{‘i Accepts a little-endian integer. Try it online! This is the naive implementation, there might be a way to get a terser code.


4

Java (JDK), 32 bytes n->"99608231754".charAt(n%41%11) Try it online! Credits Port of Arnauld's JavaScript answer, much shorter than previous attempt in the post history. -1 byte thanks to Jonathan Allan, using Misha Lavrov's Wolfram Language (Mathematica) answer.


4

Python 2, 31 bytes lambda n:'99608231754'[n%41%11] Try it online! takes input as little-endian.


3

Charcoal, score 10 * (10 + 15) = 250. Uses decimal; previous base 16-based solution scored 328 296 264. May output nonprintable characters. In particular, character 10 is tricky to input to Charcoal. Encoder, 10 bytes: ⭆⮌S℅⁺Iι×χκ Try it online! Link is to verbose version of code. Decoder, 15 bytes: IΣES×﹪℅ιχXχ÷℅ιχ Try it online! Link is to verbose ...


3

Python 3, 155 148 101 97 bytes def f(x): a=x.rfind('[') if~a:b=x.find(']',a);x=f(x[:a]+x[a+1:b]*int(x[b+1])+x[b+2:]) return x Try It Online Thanks to HyperNeutrino and Mego for -47 bytes and user202729 for -4 bytes.


3

JavaScript - 77 75 72 bytes f=a=>a.replace(/(.*)\[([^[]*?)](.)(.*)/,(a,b,c,d,e)=>f(b+c.repeat(d)+e)) Edit: updated regex with Shaggy's recommendation Snippet: const test = ["[Foo[Bar]3]2", "[one]1[two]2[three]3", "[three[two[one]1]2]3", "[!@#[$%^[&*(]2]2]2", "[[foo bar baz]1]1", "[only once]12", "[only twice]23456789", "[remove me!]0", "...


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