20
\$\begingroup\$

Write the shortest code in the language of your choice to perform run length decoding of the given string.

The string will be supplied as input on stdin in the form

CNCNCNCNCNCNCNCN

where each C could be any printable ASCII character and each N is a digit 1 to 9 (inclusive).

Sample input:

:144,1'1

Corresponding output:

:4444,'
\$\endgroup\$

49 Answers 49

28
\$\begingroup\$

Brainfuck, 34 characters

,[>,>++++++[<-------->-]<[<.>-]<,]
\$\endgroup\$
  • 5
    \$\begingroup\$ Wow. A brainfuck solution that can compete with other solutions? \$\endgroup\$ – Johannes Kuhn Oct 19 '13 at 14:49
13
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

GolfScript, 10 characters

2/{1/~~*}/
\$\endgroup\$
5
\$\begingroup\$

perl, 27 characters

print<>=~s/(.)(.)/$1x$2/ger
\$\endgroup\$
  • \$\begingroup\$ This seems unnecessarily verbose: print<>=~s/(.)(.)/$1x$2/ger. I'm also fairly sure you meant $1x$2, and not the other way around. \$\endgroup\$ – primo Oct 19 '13 at 11:17
  • \$\begingroup\$ @primo true - I didn't know about the r flag and I couldn't find it. Thanks. As for other part - sorry, I misread the spec. I'll edit when I can. \$\endgroup\$ – John Dvorak Oct 19 '13 at 11:26
  • \$\begingroup\$ BTW /r is documented in perlop and was added in v5.14.0 \$\endgroup\$ – psxls Oct 20 '13 at 13:34
  • \$\begingroup\$ Using the -p flag let you drop print and <>, so answer will become simply: s/(.)(.)/$1x$2/ge -> 17chars +1 for -p -> 18. \$\endgroup\$ – F. Hauri Dec 4 '13 at 16:52
4
\$\begingroup\$

R 67

x=strsplit(readline(),"")[[1]];cat(rep(x[c(T,F)],x[c(F,T)]),sep="")
\$\endgroup\$
  • \$\begingroup\$ +1 I had no idea rep would coerce the times argument from characters to integers automatically. Brilliant. \$\endgroup\$ – plannapus Oct 21 '13 at 6:59
4
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Python 2 raw_input matches Python 3 input. So first line must by s=input() \$\endgroup\$ – AMK Dec 8 '13 at 18:07
  • 1
    \$\begingroup\$ 49: s=input() while s:a,b,*s=s;print(a*int(b),end='') \$\endgroup\$ – Cees Timmerman Aug 23 '16 at 14:53
  • \$\begingroup\$ 46 bytes \$\endgroup\$ – movatica Aug 30 at 15:05
3
\$\begingroup\$

APL (22)

,/{⍺/⍨⍎⍵}/↑T⊂⍨~⎕D∊⍨T←⍞

Explanation:

  • T←⍞: store input in T
  • T⊂⍨~⎕D∊⍨T: split T on those characters that aren't digits
  • : turn it into a 2-by-N/2 matrix
  • {⍺/⍨⍎⍵}/: on each row of the matrix (/), replicate (/) the first character () by the eval () of the second character ()
  • ,/: concatenate the output of each row
\$\endgroup\$
3
\$\begingroup\$

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,'
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Just removed a redundant "mov ah,8" \$\endgroup\$ – Mike C Oct 20 '13 at 8:39
  • 2
    \$\begingroup\$ You should post compiled byte count rather than our assembler char count. Rules abuse FTW \$\endgroup\$ – arrdem Dec 4 '13 at 21:11
  • \$\begingroup\$ What about dq 21cdc38821cd08b4 d888ed30c188482c e8ebfce210cd14b4 for 53 characters? I don't see where it handles non-uppercase characters or eof though... \$\endgroup\$ – Jason Goemaat Dec 4 '13 at 22:17
  • \$\begingroup\$ arrdem: Good idea. I wonder if it's even breaking the rules if I hand-assemble it in a hex editor. I'd still be directly writing the code, just on a lower level than asm source. :) Jason: I don't see anything about EOF in the rules. It's stdin, just hit ctrl-c to stop it. Also why wouldn't it handle lowercase letters? \$\endgroup\$ – Mike C Dec 5 '13 at 22:24
  • \$\begingroup\$ Generally machine code is counted by byte count in comparison to source code count for interpreted or compiled languages, as there really is no reasonable alternative. \$\endgroup\$ – Joe Z. Dec 7 '13 at 5:01
2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ +1 (\2.{9} is a great idea) splendid! \$\endgroup\$ – F. Hauri Dec 4 '13 at 22:50
2
\$\begingroup\$

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);
}
\$\endgroup\$
  • \$\begingroup\$ I can't get past this with gcc: error: first parameter of 'main' (argument count) must be of type 'int'. Is there a command line switch? \$\endgroup\$ – Darren Stone Dec 4 '13 at 18:18
  • \$\begingroup\$ @DarrenStone, this code isn't 100% standard compliant. I don't use the first parameter as a parameter, so its type doesn't matter. Most compilers don't mind so much. \$\endgroup\$ – ugoren Dec 5 '13 at 20:16
  • \$\begingroup\$ Ok, thanks. I'm envious of your golf-friendlier compiler! :) \$\endgroup\$ – Darren Stone Dec 5 '13 at 21:07
2
\$\begingroup\$

Perl, 19 18 characters

perl -pe 's/(.)(.)/$1x$2/ge'

The rules for counting switches on the command-line are here.

\$\endgroup\$
2
\$\begingroup\$

Forth, 45 characters

BEGIN KEY KEY 48 - 0 DO DUP EMIT LOOP 0 UNTIL

Tested with pforth on OS X.

\$\endgroup\$
2
\$\begingroup\$

Python, 63 62 characters

print''.join([c*int(n)for c,n in zip(*[iter(raw_input())]*2)])
\$\endgroup\$
  • \$\begingroup\$ Nice trick with iter there... I think I'll use it myself! \$\endgroup\$ – boothby Dec 4 '13 at 7:24
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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);}}
\$\endgroup\$
  • \$\begingroup\$ You can shave off a character by dropping "int " and declaring c and n as parameters of main, another by using for(;;) instead of while(1), and finally two more by dropping the braces on the innermost while loop. \$\endgroup\$ – Stuntddude Dec 4 '13 at 11:09
  • \$\begingroup\$ Thanks, @Stuntddude! I applied the loop and brace suggestions, but I'm struggling with "declaring c and n as parameters of main". Still, this shaved 3 chars. Cheers. \$\endgroup\$ – Darren Stone Dec 4 '13 at 18:21
  • \$\begingroup\$ Since main() is a function, you can give it parameters, like: main(c,n){ ... } which will be passed 1 by default when the program is run. \$\endgroup\$ – Stuntddude Dec 4 '13 at 22:55
  • \$\begingroup\$ Thanks @Stuntddude. I'm aware of that and can take advantage of the 1st int arg, but compiler(s) I use complain error: second parameter of 'main' (argument array) must be of type 'char **' so I can't get away with main(c,n); I must use main(int c,char **n). Could be a platform or gcc thing. \$\endgroup\$ – Darren Stone Dec 4 '13 at 23:15
  • \$\begingroup\$ My compiler lets me do n;main(c) but not main(n,c) -- good enough! :) \$\endgroup\$ – Darren Stone Dec 4 '13 at 23:25
2
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ read[y] saves two characters \$\endgroup\$ – MtnViewMark Dec 6 '13 at 20:48
  • \$\begingroup\$ @MtnViewMark Thanks. I put it in. \$\endgroup\$ – Silvio Mayolo Dec 7 '13 at 22:54
  • \$\begingroup\$ I'm getting 57 bytes for this? You can replace replicate x y with [1..x]>>[y]. Thus your second line can be replaced with f(x:y:s)=(['1'..y]>>[x])++f s, which brings it down to 53 bytes. \$\endgroup\$ – Angs Oct 12 '16 at 16:18
2
\$\begingroup\$

Japt -P, 8 bytes

Input as an array of characters, output as a string.

ò crÈpY°

Try it

ò 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
\$\endgroup\$
  • \$\begingroup\$ Oh, c​r​e​epy!​ \$\endgroup\$ – Khuldraeseth na'Barya Jul 27 at 20:52
  • \$\begingroup\$ @Khuldraesethna'Barya, it can also be ò crÏ°îX if you find it too creepy! \$\endgroup\$ – Shaggy Jul 27 at 20:53
2
\$\begingroup\$

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!

It's working

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 6 5 bytes

2ι`ÅΓ

-1 byte thanks to @Grimy.

Outputs as a list of characters.

Try it online.

Old 6 bytes answer without run-length decode builtin:

2ôε`×?

Try it online.

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
\$\endgroup\$
  • 1
    \$\begingroup\$ 2ι`ÅΓ is 5 bytes. Would be sad if the RLE built-in didn't win an RLE challenge. \$\endgroup\$ – Grimy Aug 30 at 14:27
  • \$\begingroup\$ @Grimy Ah, that's indeed better, thanks! :) \$\endgroup\$ – Kevin Cruijssen Aug 30 at 16:50
1
\$\begingroup\$

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]))
\$\endgroup\$
1
\$\begingroup\$

GolfScript (10 chars)

2/{)15&*}/
\$\endgroup\$
1
\$\begingroup\$

J - 24

;@(_2(<@#~".)/\])@1!:1 3

The point of this submission is to use the infix adverb.

\$\endgroup\$
1
\$\begingroup\$

Befunge, 49 chars

>~:25*-      v
$>\1-:v:-*68~_@
$^ ,:\_v
^      <
\$\endgroup\$
1
\$\begingroup\$

K, 35

{,/(#).'|:'"*I"$/:(2*!-_-(#x)%2)_x}
\$\endgroup\$
  • \$\begingroup\$ ,/{(. y)#x}.'0N 2# for 18 bytes. \$\endgroup\$ – streetster Jul 26 at 11:51
1
\$\begingroup\$

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())
\$\endgroup\$
1
\$\begingroup\$

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++;}}}}
\$\endgroup\$
  • \$\begingroup\$ Use static blocks instead of a main, and compile it with Java6! \$\endgroup\$ – Fabinout Dec 4 '13 at 10:52
1
\$\begingroup\$

Befunge-98, 22 chars

>#@~~"0"-v
^#:\,:\-1<_
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.