| Bytes | Lang | Time | Link |
|---|---|---|---|
| 020 | Dyalog APL | 250905T175304Z | Aaron |
| 046 | AWK | 250905T170436Z | xrs |
| 004 | Thunno 2 J | 230709T133459Z | The Thon |
| 007 | Husk | 201012T071311Z | Razetime |
| 005 | 05AB1E | 190726T095847Z | Kevin Cr |
| 4946 | Malbolge Unshackled 20trit rotation variant | 190830T133502Z | Kamila S |
| 016 | Gema | 160822T083221Z | manatwor |
| 011 | Brachylog | 190805T190432Z | Unrelate |
| 024 | 8088 Assembly | 190729T195445Z | 640KB |
| 020 | Perl 6 | 190728T112203Z | Jo King |
| 040 | Python 2 | 190728T030513Z | Chas Bro |
| 024 | ><> | 190727T235536Z | Conor O& |
| 008 | Japt P | 190727T204542Z | Shaggy |
| 054 | SmileBASIC | 190726T151906Z | 12Me21 |
| 060 | Clojure | 190726T123518Z | NikoNyrh |
| 017 | K oK | 190726T101836Z | mkst |
| 012 | Ahead | 190726T090317Z | snail_ |
| 051 | PHP | 161012T145320Z | Jör |
| 010 | Jelly | 170708T153506Z | Erik the |
| 055 | RProgN | 161012T223509Z | ATaco |
| 406 | Shakespeare Programming Language | 160823T130200Z | Copper |
| 040 | JavaScript | 161012T192905Z | Grax32 |
| 036 | ><> 36 Bytes | 161012T171106Z | L. Steer |
| 040 | JavaScript ES6 | 161012T113434Z | Hedi |
| 142 | C++ | 160822T100958Z | Yytsi |
| 105 | C# | 160822T103640Z | Yytsi |
| 030 | Ruby | 131021T122218Z | daniero |
| 033 | Senva | 160822T082012Z | ClementN |
| 107 | Clojure | 131230T214615Z | Headmast |
| 052 | Python 3 | 131022T180449Z | boothby |
| 056 | Haskell | 131102T005917Z | Silvio M |
| 135 | Whitespace | 131206T201220Z | r.e.s. |
| 068 | C | 131104T005438Z | Darren S |
| 055 | Windows PowerShell | 131204T175328Z | goric |
| 022 | Befunge98 | 131204T105827Z | FireFly |
| 285 | Java | 131204T092229Z | masterX2 |
| 058 | Python 2 | 131022T145622Z | boothby |
| 062 | Python | 131104T014212Z | Darren S |
| 045 | Forth | 131104T002841Z | Darren S |
| 035 | K | 131101T135951Z | tmartin |
| 049 | Befunge | 131031T005938Z | saeedn |
| 018 | Perl | 131022T154819Z | breadbox |
| 065 | C | 131022T115154Z | ugoren |
| 024 | J | 131021T145751Z | jpjacobs |
| 1222 | GNU SED | 131021T105104Z | Hasturku |
| 010 | GolfScript | 131020T205330Z | Peter Ta |
| 098 | 8086 assembly | 131019T235755Z | Mike C |
| 066 | Python | 131019T113349Z | Coding m |
| 034 | Brainfuck | 131019T125506Z | alephalp |
| 027 | perl | 131019T091537Z | John Dvo |
| 067 | R | 131019T210812Z | flodel |
| 022 | APL | 131019T125021Z | marinus |
| 010 | GolfScript | 131019T091431Z | Howard |
Dyalog APL, 20 bytes
{∊{⍺/⍨⍎⍵}/↑⍵⊂⍨2|⍳≢⍵}
2|⍳≢⍵ # Find where the indices of the length of the input are odd
⍵⊂ # Partition the input by that group (every two)
↑ # Mix into a two-column matrix
{ }/ # Apply over each row
⍎⍵ # Turn the second column into a number
⍺/⍨ # Replicate the first column that many times
∊ # Flatten
💎
Created with the help of Luminespire.
Thunno 2 J, 4 bytes
d^N×
Explanation
d^N× # Implicit input -> ":144,1'1"
d # Cast to characters -> [":","1","4","4",",","1","'","1"]
^ # Uninterleave -> [":","4",",","'"] ["1","4","1","1"]
N # Cast to integer -> [":","4",",","'"] [1,4,1,1]
× # Multiply together -> [":","4444",",","'"]
# Implicit output -> :4444,'
05AB1E, 6 5 bytes
2ι`ÅΓ
-1 byte thanks to @Grimy.
Outputs as a list of characters.
Old 6 bytes answer without run-length decode builtin:
2ôε`×?
Explanation:
2ι # Uninterleave the (implicit) input-string in two parts
# i.e. ":144,1'3" → [":4,'","1413"]
` # Push both separated to the stack
ÅΓ # Run-length decode
# i.e. ":4,'" and "1413" → [":","4","4","4","4",",","'","'","'"]
# (after which the result is output implicitly)
2ô # Split the (implicit) input-string into parts of size 2
# i.e. ":144,1'3" → [":1","44",",1","'3"]
ε # Loop over each of these pairs:
` # Push both characters separated to the stack
× # Repeat the first character the digit amount of times as string
# i.e. "'" and "3" → "'''"
? # And print it without trailing newline
Malbolge Unshackled (20-trit rotation variant), 4,494e6 bytes
Size of this answer exceeds maximum postable program size (eh), so the code is located in my GitHub repository.
How to run this?
This might be a tricky part, because naive Haskell interpreter will take ages upon ages to run this. TIO has decent Malbogle Unshackled interpreter, but sadly I won't be able to use it (limitations).
The best one I could find is the fixed 20-trit rotation width variant, that performs very well, decompressing 360 bytes per hour.
To make the interpreter a bit faster, I've removed all the checks from Matthias Lutter's Malbolge Unshackled interpreter.
My modified version can run around 6,3% faster.
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char* translation = "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72Fh"
"OA1CB6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";
typedef struct Word {
unsigned int area;
unsigned int high;
unsigned int low;
} Word;
void word2string(Word w, char* s, int min_length) {
if (!s) return;
if (min_length < 1) min_length = 1;
if (min_length > 20) min_length = 20;
s[0] = (w.area%3) + '0';
s[1] = 't';
char tmp[20];
int i;
for (i=0;i<10;i++) {
tmp[19-i] = (w.low % 3) + '0';
w.low /= 3;
}
for (i=0;i<10;i++) {
tmp[9-i] = (w.high % 3) + '0';
w.high /= 3;
}
i = 0;
while (tmp[i] == s[0] && i < 20 - min_length) i++;
int j = 2;
while (i < 20) {
s[j] = tmp[i];
i++;
j++;
}
s[j] = 0;
}
unsigned int crazy_low(unsigned int a, unsigned int d){
unsigned int crz[] = {1,0,0,1,0,2,2,2,1};
int position = 0;
unsigned int output = 0;
while (position < 10){
unsigned int i = a%3;
unsigned int j = d%3;
unsigned int out = crz[i+3*j];
unsigned int multiple = 1;
int k;
for (k=0;k<position;k++)
multiple *= 3;
output += multiple*out;
a /= 3;
d /= 3;
position++;
}
return output;
}
Word zero() {
Word result = {0, 0, 0};
return result;
}
Word increment(Word d) {
d.low++;
if (d.low >= 59049) {
d.low = 0;
d.high++;
if (d.high >= 59049) {
fprintf(stderr,"error: overflow\n");
exit(1);
}
}
return d;
}
Word decrement(Word d) {
if (d.low == 0) {
d.low = 59048;
d.high--;
}else{
d.low--;
}
return d;
}
Word crazy(Word a, Word d){
Word output;
unsigned int crz[] = {1,0,0,1,0,2,2,2,1};
output.area = crz[a.area+3*d.area];
output.high = crazy_low(a.high, d.high);
output.low = crazy_low(a.low, d.low);
return output;
}
Word rotate_r(Word d){
unsigned int carry_h = d.high%3;
unsigned int carry_l = d.low%3;
d.high = 19683 * carry_l + d.high / 3;
d.low = 19683 * carry_h + d.low / 3;
return d;
}
// last_initialized: if set, use to fill newly generated memory with preinitial values...
Word* ptr_to(Word** mem[], Word d, unsigned int last_initialized) {
if ((mem[d.area])[d.high]) {
return &(((mem[d.area])[d.high])[d.low]);
}
(mem[d.area])[d.high] = (Word*)malloc(59049 * sizeof(Word));
if (!(mem[d.area])[d.high]) {
fprintf(stderr,"error: out of memory.\n");
exit(1);
}
if (last_initialized) {
Word repitition[6];
repitition[(last_initialized-1) % 6] =
((mem[0])[(last_initialized-1) / 59049])
[(last_initialized-1) % 59049];
repitition[(last_initialized) % 6] =
((mem[0])[last_initialized / 59049])
[last_initialized % 59049];
unsigned int i;
for (i=0;i<6;i++) {
repitition[(last_initialized+1+i) % 6] =
crazy(repitition[(last_initialized+i) % 6],
repitition[(last_initialized-1+i) % 6]);
}
unsigned int offset = (59049*d.high) % 6;
i = 0;
while (1){
((mem[d.area])[d.high])[i] = repitition[(i+offset)%6];
if (i == 59048) {
break;
}
i++;
}
}
return &(((mem[d.area])[d.high])[d.low]);
}
unsigned int get_instruction(Word** mem[], Word c,
unsigned int last_initialized,
int ignore_invalid) {
Word* instr = ptr_to(mem, c, last_initialized);
unsigned int instruction = instr->low;
instruction = (instruction+c.low + 59049 * c.high
+ (c.area==1?52:(c.area==2?10:0)))%94;
return instruction;
}
int main(int argc, char* argv[]) {
Word** memory[3];
int i,j;
for (i=0; i<3; i++) {
memory[i] = (Word**)malloc(59049 * sizeof(Word*));
if (!memory) {
fprintf(stderr,"not enough memory.\n");
return 1;
}
for (j=0; j<59049; j++) {
(memory[i])[j] = 0;
}
}
Word a, c, d;
unsigned int result;
FILE* file;
if (argc < 2) {
// read program code from STDIN
file = stdin;
}else{
file = fopen(argv[1],"rb");
}
if (file == NULL) {
fprintf(stderr, "File not found: %s\n",argv[1]);
return 1;
}
a = zero();
c = zero();
d = zero();
result = 0;
while (!feof(file)){
unsigned int instr;
Word* cell = ptr_to(memory, d, 0);
(*cell) = zero();
result = fread(&cell->low,1,1,file);
if (result > 1)
return 1;
if (result == 0 || cell->low == 0x1a || cell->low == 0x04)
break;
instr = (cell->low + d.low + 59049*d.high)%94;
if (cell->low == ' ' || cell->low == '\t' || cell->low == '\r'
|| cell->low == '\n');
else if (cell->low >= 33 && cell->low < 127 &&
(instr == 4 || instr == 5 || instr == 23 || instr == 39
|| instr == 40 || instr == 62 || instr == 68
|| instr == 81)) {
d = increment(d);
}
}
if (file != stdin) {
fclose(file);
}
unsigned int last_initialized = 0;
while (1){
*ptr_to(memory, d, 0) = crazy(*ptr_to(memory, decrement(d), 0),
*ptr_to(memory, decrement(decrement(d)), 0));
last_initialized = d.low + 59049*d.high;
if (d.low == 59048) {
break;
}
d = increment(d);
}
d = zero();
unsigned int step = 0;
while (1) {
unsigned int instruction = get_instruction(memory, c,
last_initialized, 0);
step++;
switch (instruction){
case 4:
c = *ptr_to(memory,d,last_initialized);
break;
case 5:
if (!a.area) {
printf("%c",(char)(a.low + 59049*a.high));
}else if (a.area == 2 && a.low == 59047
&& a.high == 59048) {
printf("\n");
}
break;
case 23:
a = zero();
a.low = getchar();
if (a.low == EOF) {
a.low = 59048;
a.high = 59048;
a.area = 2;
}else if (a.low == '\n'){
a.low = 59047;
a.high = 59048;
a.area = 2;
}
break;
case 39:
a = (*ptr_to(memory,d,last_initialized)
= rotate_r(*ptr_to(memory,d,last_initialized)));
break;
case 40:
d = *ptr_to(memory,d,last_initialized);
break;
case 62:
a = (*ptr_to(memory,d,last_initialized)
= crazy(a, *ptr_to(memory,d,last_initialized)));
break;
case 81:
return 0;
case 68:
default:
break;
}
Word* mem_c = ptr_to(memory, c, last_initialized);
mem_c->low = translation[mem_c->low - 33];
c = increment(c);
d = increment(d);
}
return 0;
}
It's working!
Gema, 17 16 characters
??=@repeat{$2;?}
Sample run:
bash-4.3$ gema '??=@repeat{$2;?}' <<< ":144,1'1"
:4444,'
Brachylog, 11 bytes
ġ₂{ẹịᵗj₎}ᵐc
c The output is the concatenation of
{ }ᵐ the result of mapping
ẹịᵗj₎ repeating the first element by the second converted to an integer
ġ₂ over the length-2 chunks of the input.
8088 Assembly, IBM PC DOS, 24 bytes
Binary xxd dump:
00000000: d1ee add0 e88a c8ad 80ec 308a dcb4 0ecd ..........0.....
00000010: 10fe cb75 f8e2 f0c3 ...u....
Unassembled listing:
D1 EE SHR SI, 1 ; point SI to DOS PSP (080H)
AD LODSW ; load input length into AL
D0 E8 SHR AL, 1 ; half input length to get number of CN pairs
8A C8 MOV CL, AL ; move into counter
C_LOOP:
AD LODSW ; Load pair: C = AL, N = AH
80 EC 30 SUB AH, '0' ; ASCII convert N to binary
8A DC MOV BL, AH ; BL is counter for number of reps of C
N_LOOP:
B4 0E MOV AH, 0EH ; BIOS teletype output function
CD 10 INT 10H ; call BIOS, display C
FE CB DEC BL ; decrement loop counter
75 F8 JNZ N_LOOP ; if end of N loop, move to next C
E2 F0 LOOP C_LOOP ; loop through all C's
C3 RET ; return to DOS
Standalone DOS executable program. Input via command line, output to console.
Perl 6, 20 bytes
{S:g{(.)(.)}=$0 x$1}
Anonymous codeblock containing a regex that replaces each pair of characters in the input with the first one repeated by the second.
><>, 28 24 bytes
Saved 4 bytes thanks to Jo King!
/1-:?v
\$o:$<-*c4i;?(0:i
This was very interesting to golf the form. You can see my process below.
Alternatives
i:0(?;i4c*->: ?v
^$o:$-1<
/? <:-*c4i;?(0:i
\1-$:o$:^0
/? :<-*c4i;?(0:i
\1-$:o$^.00
/? :<-*c4ip1c;?(0:i
\1-c1go^.00
/? :<-*c4ip1c;?(0:i
\1-c1go^.00
/?}-1<:-*c4i;?(0:i[0
\:o}:^.00
/?}-1<:-*c4i;?(0:i[0
\:o}:^00
0/?}-1<:-*c4i;?(0:i[
0\:o}:^
Japt -P, 8 bytes
Input as an array of characters, output as a string.
ò crÈpY°
ò crÈpYn :Implicit input of character array
ò :Groups of 2
r :Reduce each pair
È :By passing them through the following function as [X,Y]
p : Repeat X
Yn : Y, converted to an integer, times
:Implicitly join and output
SmileBASIC, 54 bytes
LINPUT S$WHILE I<LEN(S$)?S$[I]*VAL(S$[I+1]);
I=I+2WEND
Alternative (same size):
LINPUT S$WHILE""<S$?SHIFT(S$)*VAL(S$[1]);
S$[0]="
WEND
Really wanted to write ?SHIFT(S$)*VAL(SHIFT(S$)) but expressions are evaluated right to left, so the shifts would happen in the wrong order...
Clojure, 60 bytes
#(apply str(for[[c n](partition 2 %)_(range(-(int n)48))]c))
I assume we can take the input as an argument and return the string. If not this grows to 73 bytes:
#(pr(apply str(for[[c n](partition 2(read-line))_(range(-(int n)48))]c))
K (oK), 17 bytes
Solution:
,/{(.y)#x}.'0N 2#
Explanation:
,/{(.y)#x}.'0N 2# / the solution
0N 2# / reshape input into Nx2 grid
{ }.' / apply lambda to each pair of inputs
#x / take from x
( ) / do this together
.y / value y (ie "5" => 5)
,/ / flatten
Ahead, 12 bytes
~W:k-1'ii@j~
Explained
Single line Ahead boards are nice because you can explain them without some kind of diagram.
(Program effectively loops in reverse by floating)
~ float to next ~ (to end of line)
~ stop floating (end of line, turn around)
j jump next
i read char from input
@ bounce here and die if input is empty, else continue west
i read digit char from input
-1' subtract '1 (49) from input char to get digit value
: dup first char that many times
W Write stack to output as chars
~ (repeat until head dies from empty input)
PHP, 51 bytes
for(;--$z?:($c=$argn[$i++]).$z=$argn[$i++];)echo$c;
PHP, 54 bytes
for(;~$c=$argn[$i++];)echo str_repeat($c,$argn[$i++]);
PHP, 54 bytes
for(;~$c=$argn[$i++];)echo str_pad($c,$argn[$i++],$c);
RProgN, 55 Bytes
S y = '' x = y L ¿ y pop y pop \ rep x . 'x' = y L } x
Explained
S y = # Convert the input to a backwards stack, assign it to y.
'' x = # Assign a blank string to x
y L # Push the length of y
¿ # While the top of the stack is truthy, popping.
y pop # Push the top value of y to the stack.
y pop # Twice.
\ # Swap them around, so we get C 1 instead of 1 C
rep # Repeat the string. (C 1 times in the above example)
x . 'x' = # Append it to the back of x, and re-assign it. We use the back because we're starting at the END of the string.
y L # Push the length of y for the next itteration of the loop.
} #
x # Push x, and implicitly print it.
Shakespeare Programming Language, 406 bytes
.
Ajax,.
Ford,.
Act I:.
Scene I:.
[Enter Ajax and Ford]
Scene II:.
Ford:
Open your mind.Is sky nicer than you?If so, let us return to scene IV.
Ajax:
Open your mind.You is sum you and sum big big big big big big pig and big big big big cat!
Scene III:.
Ford:
Speak thy mind.
Ajax:
You is sum you and pig!Is you as big as zero?If so, let us return to scene II.Let us return to scene III.
Scene IV:.
[Exeunt]
Ungolfed version:
The Decoding of the Lengths of Veronan Runs - A Drama of PPCG.
Romeo, quite a character.
Juliet, Romeo's lover and multiplicand.
Act I: In which the lengths of runs are decoded.
Scene I: A silent entrance.
[Enter Romeo and Juliet]
Scene II: In which neither Romeo nor Juliet believes the other open-minded.
Juliet:
Open your mind. Is my mother jollier than thou? If so,
we must proceed to scene IV.
Romeo:
Open your mind. Thou art the sum of thyself and the sum of my good aunt and
the difference between nothing and the quotient of the square of twice the sum
of thy foul fat-kidneyed goat and thy death and thy evil variable!
Scene III: In which Romeo snaps and brutally insults Juliet.
Juliet:
Speak thy mind.
Romeo:
Thou art the sum of thyself and a hog! Art thou as rotten as nothing? If so,
let us return to scene II. Let us return to scene III.
Scene IV: Finale.
[Exeunt]
I'm using drsam94's Python SPL compiler, which has a few bugs (which is why, for instance, I use Open your mind instead of Open thy mind in the golfed version).
To run this program, use:
$ python splc.py rld.spl > rld.c
$ gcc rld.c -o rld.exe
$ echo -n ":144,1'1" | ./rld
:4444,'
How it works
SPL is an esoteric programming language designed to make programs look like Shakespeare plays. It does this by using characters as variables, and processing is performed by having the characters say things to one another.
The Decoding of the Lengths of Veronan Runs - A Drama of PPCG.
This is the title of the play; it's ignored by the compiler.
Romeo, quite a character.
Juliet, Romeo's lover and multiplicand.
Here we're declaring the variables used in the rest of the program. Everything betwen , and . is ignored by the compiler. In this case, we declare Romeo, used to hold the character being decoded, and Juliet, used to hold the run length of the character.
Act I: In which the lengths of runs are decoded.
Here we declare the first and only act in the program. Acts and scenes are like labels; they can be jumped to at any time by using let us return to scene II or some variant of that. We only use one act, because it's sufficient for our needs. Again, anything between : and . is ignored by the compiler.
Scene I: A silent entrance.
Here we declare the first scene. Scenes are numbered in Roman numerals: the first is Scene I, the second Scene II, and so on.
[Enter Romeo and Juliet]
This is a stage direction; in it, we tell the Romeo and Juliet variables to come onto the "stage". Only two variables can be on the "stage" at once; the stage is used so that the compiler can figure out which variable is addressing which when they speak. Because we have only two variables, Romeo and Juliet will stay onstage for the length of the program.
Scene II: In which neither Romeo nor Juliet believes the other open-minded.
Another scene declaration. Scene II will be jumped to in order to decode another run-length.
Juliet:
This form of declaration means that Juliet is going to start speaking. Everything until the next Romeo:, stage direction, or scene/act declaration will be a line spoken by Juliet, and thus "me" will refer to Juliet, "you"/"thou" to Romeo, etc.
Open your mind.
This command stores the ordinal value of single character from STDIN in Romeo.
Is my mother jollier than thou?
In SPL, nouns translate to either 1 or -1 depending on whether they are positive or negative. In this case, my mother translates to 1. Adjectives (positive or negative) multiply their noun by 2.
This is a question; in it, Juliet asks if my mother (AKA 1) is "jollier" than Romeo. Comparatives either translate to less than (if they are negative, like worse) or greater than (if they are positive, like jollier). Therefore, this question boils down to Is 1 greater than you?.
The reason we ask this question is to detect the end of the input. Since the value of EOF varies by platform, but is usually less than 1, we use this to detect it.
If so, we must proceed to scene IV.
If the preceding question evaluated to true, we jump to scene IV—which is simply the end of the program. In short, if we detect an EOF, we end the program.
Romeo:
It's now Romeo's line: "me" and "you" refer to Romeo and Juliet, respectively.
Open your mind.
Again, this statement puts the ordinal value of a single character from STDIN into Juliet, which in this case is the run-length of the character stored in Romeo.
Thou art the sum of thyself and the sum of my good aunt and the difference
between nothing and the quotient of the square of twice the sum of thy foul
fat-kidneyed goat and thy death and thy evil variable!
This one's too long to go over in great detail, but just trust me in that it translates to Juliet -= 48. We do this because Juliet holds the ASCII value of a numeral, and ord('0') == 48; in subtracting 48, we translate from the ASCII value of a number to the number itself.
Scene III: In which Romeo snaps and brutally insults Juliet.
Another scene declaration. This one is for the loop in which we repeatedly print the character value of Romeo, Juliet times.
Juliet:
Speak thy mind.
This statement causes Romeo to print his value as a character; that is, whatever character value was previously stored in Romeo is now output.
Romeo:
Thou art the sum of thyself and a hog!
A hog is a negative noun, so a hog translates to -1; therefore, this statement evaluates to Juliet -= 1.
Art thou as rotten as nothing?
Romeo here asks if Juliet is "as rotten as", or equal to, 0.
If so, let us return to scene II.
If Juliet's value is 0, we loop back to scene II to decode another character's run-length.
Let us return to scene III.
Else, we loop back to scene III to output Romeo's character again.
Scene IV: Finale.
[Exeunt]
This final scene declaration is just a marker for the end of the program. The [Exeunt] stage direction is necessary to get the compiler to actually generate the final scene.
JavaScript, 40 bytes
f=v=>v&&v[0].repeat(v[1])+f(v.slice(2))
Recursive. Expand first 2 characters and call self with remainder of string.
><> - 36 Bytes
i:01-=?; l2=?v
v?=*68:ro:r-1<
>~~d0.
This solution can be run without using the input flag or preloading the stack, although both of those methods could cut down on bytes. If there is any interest, I can describe how it works to anyone who is curious.
Try it here!
JavaScript (ES6), 40 bytes
s=>s.replace(/../g,v=>v[0].repeat(v[1]))
f=
s=>s.replace(/../g,v=>v[0].repeat(v[1]))
;
console.log(f(":144,1'1"))
C++ - 151 144 142 bytes
#include <iostream>
#include <string>
void f(){std::string p;std::cin>>p;for(int i=0,j;i<p.size();i+=2)for(j=48;j<p[i+1];j++)std::cout<<p[i];}
Way too long. I should probably just use an character pointer instead and forget the whole string thing.
2 bytes saved by @PeterTaylor!
Explanation:
void f()
{
std::string p; // A string to hold the input.
std::cin >> p; // Read the input from STDIN, and source to <p>.
// Loop until the end of the string <p>, withs index jumps of 2 at each iteration.
for(int i = 0, j; i < p.size(); i += 2)
// Loop (p[i + 1] - 48) times, and print the current character each time.
/* p[i + 1] - 48 is a neat way to convert the next character in the string
to its integer digit from. */
for(j = 48; j < p[i + 1]; j++)
std::cout << p[i]; // Print the character we are at.
}
C# - 108 105 bytes
void f(){var s=Console.ReadLine();for(int i=0;i<s.Length;i+=2)Console.Write(new string(s[i],s[i+1]-48));}
Explanation:
void f()
{
var s = Console.ReadLine(); // Read the input from STDIN.
// Loop until the end of the string, and increment our index counter by 2 at each iteration.
for (int i = 0; i < s.Length; i += 2)
Console.Write(new string(s[i], s[i + 1] - 48)); // Create (s[i + 1] - 48) copies of the current character, and print it.
// s[i + 1] - 48 implicitly casts the character at s[i + 1] to its integer digit form.
}
Ruby, 30 bytes
gsub!(/(.)(.)/){$1*$2.to_i}
27 bytes code + 3 bytes to run it with the -p flag:
$ ruby -p rld.rb <<< ":144,1'1"
:4444,'
Senva, 33 bytes
I was first making a big answer with a large code into the back-memory, but finally I made that :
1'(`>0.>{@}0'{v}1'0,>48-0,-<~>$>$
Here is an explanation :
1' // Go to the second cell, the first will be used to store an information
( // Input a string
` // Go to the last cell (at the last character of the string)
> // Make a new cell after the string
0. // Store a 0 to mark the end of the string
> // Make a new cell
{@} // Store this adress into the back-memory
0' // Go to the 1st cell
{v} // Paste the adress, now the 1st cell contains the adress which points after the '0' which marks the end of the string
1' // Go to the 2nd cell (the beginning of the string)
0, // While we do NOT encounter a zero (while the string is not terminated)
> // Go to the next cell (the repeat number)
48- // Substract 48 to get the number (the string was stored as an ASCII chain)
0, // While this cell is not equal to 0 (higher than 0)
- // Decrease the cell
< // Go to the cell which contains the character to repeat
~ // Display it
> // Go to the number
$ // End of the loop
> // Go to the next number
$ // End of the loop
So we make a loop across the string, and a second loop into which display the C character `` times.
Clojure (107)
(pr(apply str(map #(apply str(repeat(Integer/parseInt(str(second %)))(first %)))(partition 2(read-line)))))
This feels exceptionally long for being Clojure, if someone can do better, please post it.
Python 3, 52
Python 3 allows me to merge the approaches of my two python2 solutions.
s=input()
t=''
while s:a,b,*s=s;t+=a*int(b)
print(t)
Haskell, 58 56 characters
f[]=[]
f(x:y:s)=replicate(read[y])x++f s
main=interact$f
My first real attempt at golfing anything, so there's probably some improvement to be made here.
Whitespace, 135
LSSSLSSSSLSLSTLTSTTTSLSSSSTSSSSLTSSTLTTTTLSSSSLSLSTLTSTTTSSSTTSSSSLTSSTLSSSSLSLSLTSTLSSSTLTSSTSTSSTLTLSSLSLSSLLSSTLSLLSLLLSLSLLSSTTLLLL
(Replace S,T,L with Space,Tab,Linefeed characters.)
Try it online [here].
Explanation:
"assembly" whitespace stack
---------- ---------- -----
s: LSS SL ;input loop []
push 0 SS SSL [0]
dup SLS [0,0]
getc TLTS ;input & store char c [0]
rcl TTT ;recall c [c]
dup SLS [c,c]
push 16 SS STSSSSL [c,c,16]
sub TSST [c,c-16]
jlt tt LTT TTL ;exit if ord(c) < 16 [c]
push 0 SS SSL [c,0]
dup SLS [c,0,0]
getc TLTS ;input & store char n [c,0]
rcl TTT ;recall n [c,n]
push 48 SS STTSSSSL ;convert n to m = ord(n)-ord('0') [c,n,48]
sub TSST [c,m]
ss: LSS SSL ;inner loop outputs c, m times [c,m]
dup SLS [c,m,m]
jeq t LTS TL ;if m==0, stop outputting this c [c,m]
push 1 SS STL ;otherwise decr m [c,m,1]
sub TSST [c,m-1]
copy 1 STS STL ;copy c to tos [c,m-1,c]
putc TLSS ;output this c [c,m-1]
jmp ss LSL SSL ;loop back to output this c again [c,m-1]
t: LSS TL [c,m]
pop SLL [c]
pop SLL []
jmp s LSL SL ;loop back to get the next c,n []
tt: LSS TTL [c]
end LLL ;exit
C, 68 characters
@ugoren's answer in C is slightly shorter, but this answer complies with the requirement that "the string will be supplied as input on stdin."
n;main(c){for(;;){c=getchar(),n=getchar()-48;while(n--)putchar(c);}}
Windows PowerShell, 55 chars
-join((read-host)-split'(..)'|%{(""+$_[0])*(""+$_[1])})
I get the feeling that this can be golfed down more, specifically with the casts from char to string and int, but I don't have the time to keep working on it right now.
Befunge-98, 22 chars
>#@~~"0"-v
^#:\,:\-1<_
Java: 285 charas
import java.util.Scanner;public class A{public static void main(String args[]){Scanner s = new Scanner(System.in);while(s.hasNext()){String t=s.next();for(int i=0;i<t.length();i++) {for(int j=0; j<(Byte.valueOf(t.substring(i+1,i+2)));j++){System.out.print(t.substring(i,i+1));}i++;}}}}
Python 2, 58
This is inspired by Darren Stone's python solution -- iterator abuse!
x=iter(raw_input())
print''.join(a*int(next(x))for a in x)
This is my original solution (60 chars)
s=raw_input()
t=''
while s:t+=s[0]*int(s[1]);s=s[2:]
print t
A different approach is 3 chars longer:
f=lambda a,b,*x:a*int(b)+(x and f(*x)or'')
print f(raw_input())
Python, 63 62 characters
print''.join([c*int(n)for c,n in zip(*[iter(raw_input())]*2)])
Forth, 45 characters
BEGIN KEY KEY 48 - 0 DO DUP EMIT LOOP 0 UNTIL
Tested with pforth on OS X.
K, 35
{,/(#).'|:'"*I"$/:(2*!-_-(#x)%2)_x}
Befunge, 49 chars
>~:25*- v
$>\1-:v:-*68~_@
$^ ,:\_v
^ <
Perl, 19 18 characters
perl -pe 's/(.)(.)/$1x$2/ge'
The rules for counting switches on the command-line are here.
C, 65 chars
Gets the input as a parameter.
main(p,v)char*p,**v;{
for(p=v[1];*p;--p[1]<49?p+=2:0)putchar(*p);
}
J - 24
;@(_2(<@#~".)/\])@1!:1 3
The point of this submission is to use the infix adverb.
GNU SED, 122 + 2 (-r)
#n
s/.*/\n&\a987654321\v\v\v\v\v\v\v\v\v/
:a
s/\n(.)(.)(.*\a.*\2.{9}(.*))/\1\n\4\3/
tb
bc
:b
s/(.)\n\v/\1\1\n/
tb
ba
:c
P
Needs to be run with the -r flag
May be reduced to 110 + 2 by replacing \v with the unprintable 0x0B and \a with 0x07
GolfScript (10 chars)
2/{)15&*}/
8086 assembly, 106 98 characters
l:
mov ah,8
int 21h
mov bl,al
int 21h
sub al,48
mov cl,al
xor ch,ch
mov al,bl
mov ah,14
p:
int 10h
loop p
jmp l
If the numbers were before the characters in the input stream, two lines (18 characters) could be shaved off of this.
Python, 78 72 66 char
d=raw_input()
print"".join([x*int(d[i+1])for i,x in enumerate(d)if~i&1])
s=raw_input() print"".join(i*int(j)for i,j in zip(s[::2],s[1::2]))
Brainfuck, 34 characters
,[>,>++++++[<-------->-]<[<.>-]<,]
perl, 27 characters
print<>=~s/(.)(.)/$1x$2/ger
R 67
x=strsplit(readline(),"")[[1]];cat(rep(x[c(T,F)],x[c(F,T)]),sep="")
APL (22)
,/{⍺/⍨⍎⍵}/↑T⊂⍨~⎕D∊⍨T←⍞
Explanation:
T←⍞: store input inTT⊂⍨~⎕D∊⍨T: splitTon those characters that aren't digits↑: turn it into a2-by-N/2matrix{⍺/⍨⍎⍵}/: on each row of the matrix (/), replicate (/) the first character (⍺) by the eval (⍎) of the second character (⍵),/: concatenate the output of each row
GolfScript, 10 characters
2/{1/~~*}/

