Let's do some "deciph4r4ng"

57

6

In this challenge, your task is to decipher a string. Luckily, the algorithm is pretty simple: reading from left to right, each encountered digit N (0 to 9) must be replaced with the character which is N+1 positions before it.

Example

The input string "Prog2am0in6" would be decoded this way:

example

Hence, the expected output is "Programming".

Clarifications and rules

  • The input string will contain ASCII characters in the range 32 - 126 exclusively. You can assume that it will never be empty.
  • The original deciphered string is guaranteed not to contain any digit.
  • Once a character has been decoded, it may in turn be referenced by a subsequent digit. For instance, "alp2c1" should be decoded as "alpaca".
  • References will never wrap around the string: only previous characters can be referenced.
  • You can write either a full program or a function, which either prints or outputs the result.
  • This is code golf, so the shortest answer in bytes wins.
  • Standard loopholes are forbidden.

Test cases

Input : abcd
Output: abcd

Input : a000
Output: aaaa

Input : ban111
Output: banana

Input : Hel0o W2r5d!
Output: Hello World!

Input : this 222a19e52
Output: this is a test

Input : golfin5 3s24o0d4f3r3y3u
Output: golfing is good for you

Input : Prog2am0in6 Puz0les7&1Cod74G4lf
Output: Programming Puzzles & Code Golf

Input : Replicants 4re3lik448ny3oth8r5mac6in8.8T64y'r371it9376a1b5n1fit7or2a1h2z17d.
Output: Replicants are like any other machine. They're either a benefit or a hazard.

Arnauld

Posted 2017-04-07T13:17:16.727

Reputation: 111 334

Can we receive the input as an array of single character strings? Can we assume that the number will never be greater than 9? – fəˈnɛtɪk – 2017-04-07T13:29:51.077

@fəˈnɛtɪk Regarding the input format: I'd say no, unless this is the only acceptable format for your language. We're dealing with single digits rather than numbers. So yes: it's guaranteed to be <= 9 but you may encounter several digits in a row. – Arnauld – 2017-04-07T13:38:18.853

Would 1bbab be a valid input (with expected output of abbab)? In other words, can the references wrap around the string? – Luke – 2017-04-07T13:41:47.353

@Luke Good point. No, 1bbab is not valid. I've added a clarification about that. – Arnauld – 2017-04-07T13:48:08.787

Answers

11

Jelly, 9 7 bytes

~ịṭṭµ@/

Try it online!

How it works

~ịṭṭµ@/  Main link. Argument: s

    µ    Combine the four links to the left into a chain (arity unknown).
     @   Swap the chains arguments. This makes it dyadic.
      /  Reduce s by the chain with swapped arguments. It will be called with
         right argument r (the result of the previous call, initially the first 
         character) and left argument c (the next character of s).
~            Bitwise NOT of c. This maps a digit 'd' to ~d = -(d+1), but all 
             non-digit characters 'D' to 0.
  ṭ          Tack; append c to r.
 ị           Index; select the character of the result to the right at the
             index from the result to the left. Indexing is 1-based and modular,
             so 0 is the last character, -1 the second to last, etc.
   ṭ         Tack; append the resulting character to r.    

Dennis

Posted 2017-04-07T13:17:16.727

Reputation: 196 637

13

Java 7, 81 80 bytes

void a(char[]a){for(int i=0;++i<a.length;)if(a[i]>47&a[i]<58)a[i]=a[i-a[i]+47];}

Try it online!

Saved 1 byte thanks to Anders Tornblad. The first character cannot be a digit so it doesn't need to be checked meaning we can preincrement before checking our terminate condition.

Poke

Posted 2017-04-07T13:17:16.727

Reputation: 3 075

2Since the first char could never contain a digit, you don't have to check it. Therefore, your loop can be for(int i=0;++i<a.length;){ instead, saving one char. – Anders Tornblad – 2017-04-10T10:35:30.423

12

Haskell, 55 bytes

o#c|c>'/',c<':'=o!!read[c]:o|1<2=c:o
reverse.foldl(#)[]

Usage example: reverse.foldl(#)[] $ "Prog2am0in6 Puz0les7&1Cod74G4lf" -> "Programming Puzzles & Code Golf". Try it online!

Reduce the string to a reverse copy of itself with the numbers replaced by the corresponding chars. "reverse", because this way we have easy access to the string so far when indexing the numbers. Reverse it again.

nimi

Posted 2017-04-07T13:17:16.727

Reputation: 34 639

1Wow, I have written this exact solution but I was slow in posting it :) Well, at least now I know it was a good one, +1 – Leo – 2017-04-07T15:20:37.787

11

C, 46 bytes

f(char*s){for(;*s++;)*s=s[(*s-52)/6?0:47-*s];}

Try it online!


C,  52   49  48 bytes

Thanks to @l4m2 for saving a byte!

f(char*s){for(;*s++;)*s>47&*s<58?*s=s[47-*s]:0;}

Edits the input string directly.

Try it online!

Alternative 50-byte version:

f(char*s){for(;*s++;)*s=abs(*s-57)>9?*s:s[47-*s];}

Recursive version, 48 bytes:

f(char*s){*s>47&*s<58?*s=s[47-*s]:0;*s++&&f(s);}

Steadybox

Posted 2017-04-07T13:17:16.727

Reputation: 15 798

9

05AB1E, 11 bytes

vydiÂyèëy}J

Try it online!

Explanation

v            # for each character y in input
 ydi         # if y is a digit
    Â        #    push a reversed copy of the string we've built up so far
     yè      #    push the character at index y in the reversed string
       ë     # else
        y    #    push y
         }   # end if
          J  # join stack to a single string
             # output top of the stack at the end of the loop

Emigna

Posted 2017-04-07T13:17:16.727

Reputation: 50 798

I really need to check to see if you've already answered more often before starting. – Magic Octopus Urn – 2017-04-10T18:12:36.950

@carusocomputing: You could still think up some better trick than I've used ;) – Emigna – 2017-04-10T18:14:35.003

7

JavaScript (ES6), 59 53 bytes

f=x=>/\d/.test(x)?f(x.replace(/\d/,(m,o)=>x[o+~m])):x

Saved 7 bytes thanks to fəˈnɛtɪk.

f=x=>/\d/.test(x)?f(x.replace(/\d/,(m,o)=>x[o+~m])):x

console.log(f("Prog2am0in6"));
console.log(f("abcd"));
console.log(f("a000"));
console.log(f("ban111"));
console.log(f("Hel0o W2r5d!"));
console.log(f("this 222a19e52"));
console.log(f("golfin5 3s24o0d4f3r3y3u"));
console.log(f("Prog2am0in6 Puz0les7&1Cod74G4lf"));
console.log(f("Replicants 4re3lik448ny3oth8r5mac6in8.8T64y'r371it9376a1b5n1fit7or2a1h2z17d."));

Tom

Posted 2017-04-07T13:17:16.727

Reputation: 3 078

.charAt(...) can be replaced with [...] for savings of 7 bytes – fəˈnɛtɪk – 2017-04-07T13:56:10.570

x.charAt(...) is equivalent to x[...] – fəˈnɛtɪk – 2017-04-07T14:44:12.453

@fəˈnɛtɪk Yep, I thought I tried that before but it threw an error. Thanks! – Tom – 2017-04-07T14:45:10.037

1o-m-1 can be replaced with o+~m. – Neil – 2017-04-07T16:15:12.077

2Because f is called recursively, the character count of the program must include the f= part, so this is 54 bytes, not 52. – user5090812 – 2017-04-07T18:19:56.707

f=x=>x!=(y=x.replace(/\d/,(m,o)=>x[o+~m]))?f(y):x – l4m2 – 2018-04-23T10:22:39.397

You can actually abuse x again: f=x=>x!=(x=x.replace(/\d/,(m,o)=>x[o+~m]))?f(x):x – Neil – 2018-04-23T10:49:00.650

then x=>f=x!=(x=x.replace(/\d/,(m,o)=>x[o+~m]))?f():x – l4m2 – 2018-04-23T19:08:01.963

5

Retina, 37 bytes

Byte count assumes ISO 8859-1 encoding.

\d
$*«»
r1+`(?<=(.)(?<-2>.)*)(«)*»
$1

Try it online!

Explanation

\d
$*«»

Replace each digit d with d «s, followed by one ». We need the latter a) to be able to recognised positions where d = 0 and b) as a separator between adjacent digits.

r1+`(?<=(.)(?<-2>.)*)(«)*»
$1

Repeatedly (+) match the regex on the first line from right to left (r) and then replace the left-most match (1) with the substitution on the second line.

The regex itself matches one of our now unary digits and counts the number of «s in group 2. The lookbehind then matches d characters with (?<-2>.)* before capturing the referred-to character in group 1. The string of «s and » is then replaced with the captured character.

Martin Ender

Posted 2017-04-07T13:17:16.727

Reputation: 184 808

5

MATL, 21 19 17 16 bytes

"@t4Y2m?UQ$y]]&h

Try it at MATL Online!

Explanation

        % Implicitly grab input as a string
"       % For each character in the input
  @     % Push that character to the stack
  t     % Make a copy of it
  4Y2   % Push the pre-defined array '0123456789' to the stack
  m     % Check if the current character is part of this array (a digit)
  ?     % If it is
    UQ  % Convert it to a number and add 1 (N)
    $y  % Make a copy of the element N-deep in the stack. MATL uses one-based indexing
        % So 1$y is the element at the top of the stack, 2$y is the next one down, etc.
  ]     % End of if statement
        % Non-digit characters remain on the stack as-is
]       % End of for loop
&h      % Horizontally concatenate the entire stack to form a string
        % Implicitly display the result

Suever

Posted 2017-04-07T13:17:16.727

Reputation: 10 257

Nice use of $y in the new version! – Luis Mendo – 2017-04-07T15:11:58.867

@LuisMendo Thanks! Yea stack-based languages are a good fit for this challenge – Suever – 2017-04-07T15:13:10.773

@LuisMendo Unfortunately this could have been shortened even more if U only worked for digits. Unfortunately 'e'U yields exp(1) otherwise I could have gotten rid of the 4Y2 stuff – Suever – 2017-04-07T15:14:49.007

Another of those Octave things... – Luis Mendo – 2017-04-07T15:16:49.740

4

JavaScript (ES6), 51 bytes

f=
s=>s.replace(/\d/g,(c,i)=>a[i]=a[i+=~c]||s[i],a=[])
<input oninput=o.textContent=f(this.value)><pre id=o>

a is used to store the replaced digits to deal with digits referring to other digits.

Neil

Posted 2017-04-07T13:17:16.727

Reputation: 95 035

s=>s.replace(a=/\d/g,(c,i)=>a[i]=a[i+=~c]||s[i]) – l4m2 – 2018-04-23T10:24:21.373

3

Perl 5, 34 bytes

33 bytes of code + -p flag.

s/\d/substr$_,-$&-1+pos,1/e&&redo

Try it online!

s/\d/.../e replace the first digit by ... evaluated as Perl code. (with ... being substr$_,-$&-1+pos,1 in that case. substr$_,-$&-1+pos,1 returns the substring of $_ of length 1 at index -$&-1+pos, where $& is the number just matched, and pos is the index of the start of the match. We just need to redo if the replace was successful in order to replace every digit. (and the result is implicitly printed thanks to -p flag).


Old approach, 47 bytes:

44 bytes of code + -F flag.

map{$F[$i]=$F[$i-$_-1]if/\d/;++$i}@F;print@F

Try it online!

Quite straight forward actually. -F flag splits the inputs on each character into @F. map{...}@F iterates through @F (ie. every character of the input). If the character if a digit (/\d/), then we replace it by the character at index $i-$_-1. The $i is the current index variable (that we maintain by incrementing at each character seen).

Dada

Posted 2017-04-07T13:17:16.727

Reputation: 8 279

3

JavaScript ES6, 61 59 bytes

Thanks @Luke for golfing off 8 bytes

x=>[...x].map((p,i,a)=>a[i]=/\d/.test(p)?a[i-1-p]:p).join``

Try it online!

fəˈnɛtɪk

Posted 2017-04-07T13:17:16.727

Reputation: 4 166

x.split\`` could also be [...x], [0-9] could be \d, together saving 6B – Luke – 2017-04-07T13:43:50.577

Currently has an error somewhere so going to fix that first – fəˈnɛtɪk – 2017-04-07T13:44:27.860

x=>[...x].map((p,i,a)=>+p+1?a[i-1-p]:p).join\`` for 46 bytes – Luke – 2017-04-07T14:05:43.707

Fails for spaces +" " gives 0 which causes it to grab the previous character – fəˈnɛtɪk – 2017-04-07T14:12:35.153

x=>[...x].map((p,i,a)=>a[i]=1+p>9?a[i-1-p]:p).join`` – l4m2 – 2018-04-23T10:17:12.603

3

05AB1E, 27 17 bytes

vyDdiU)DRXèU`X}}J

Try it online!

vy             }  # For each character
  Dd              #   Push is_number
    i         }   #   If it is
     U            #     Save save it
      )DR         #     Wrap the (reversed) stack into an array
         Xè       #     Get the character at the saved index
           U`X    #     Flatten the whole stack
                J # Join 

Riley

Posted 2017-04-07T13:17:16.727

Reputation: 11 345

2

Python 2, 75 71 bytes

s='';j=-1
for i in input():s+=s[j-int(i)]if'/'<i<':'else i;j+=1
print s

Try it Online!

Edit: Fixed for ascii values between 32-47 ; Fixed for double decoding (eg. "alp2c1" to "alpaca")

math junkie

Posted 2017-04-07T13:17:16.727

Reputation: 2 490

1@Arnauld Nope. Sorry, i didn't read the spec closely enough. WIll modify shortly – math junkie – 2017-04-07T14:14:50.180

It seems there is a bug. for 'Prog2am0in6 Puz0les7&1Cod74G4lf' your program prints Programming Puzzles &7Code1Golf! I tried with both TIO links shared! – Keerthana Prabhakaran – 2017-04-07T14:53:27.953

@KeerthanaPrabhakaran Thanks! Fixed at the cost of 0 bytes! (My alternate solution didn't make the cut though) – math junkie – 2017-04-07T15:01:14.370

Thats a great approach! – Keerthana Prabhakaran – 2017-04-07T15:08:06.240

Can you explain '/'<i<':'. I know it's testing if it's a number but how does it work? – Matias K – 2017-04-07T17:22:33.650

@MatiasK it compares the ascii value of i to / and :. Take a look at this table and you'll see why it works

– math junkie – 2017-04-07T17:26:36.330

That is a really neat way to test for integer-ness of a string! – Matias K – 2017-04-09T00:08:11.837

2

CJam, 13 bytes

q{_A,s#)$\;}/

Online demo.

This solution uses CJam's built-in "copy n-th item on the stack" operator $ to implement the decoding. It starts by reading the input (with q) and then looping over the characters from the input string and dumping them onto the stack (with {}/). However, inside the loop body it also duplicates each character after it has been put on the stack (with _) and checks if it's a digit by looking up its position with # in the string "0123456789", conveniently represented as A,s.

The result of this lookup is either the digit's numeric value or, if the character is not a digit, -1. The ) operator then increments that value by one, and $ replaces it with the character current at that many positions below the top of the stack. Finally, \; just removes the copy of the current input character that we made with _ from the stack, as it's no longer needed.

Ilmari Karonen

Posted 2017-04-07T13:17:16.727

Reputation: 19 513

2

Befunge-98, 45 43 bytes

::::#@~\1p:1g::'9`!\'/`*j;'/--1g\1p\1g#;,1+

Try it online!

The idea:

  1. For each char in the input string,
    1. Write it to line 2
    2. If it is not a number, just output it
    3. Otherwise, look up the correct value, rewrite it, then output it
::::            ; There's a counter on the stack, duplicate it 4 times  ;
    #@~         ; Get the next char of input, exiting if there is none  ;
       \1p      ; At the location (counter, 1), write the input char    ;
          :1g   ; Re-obtain the char. Stack is now [counter * 4, input] ;

::                ; Stack: [counter * 4, input * 3]      ;
  '9`!\'/`*       ; If !(input > '9') and (input > '/')  ;
                  ; IE If ('0' <= input && input <= '9') ;
           j;...; ; Then execute the ...                 ;

; Stack: [counter * 4, input] ;
; The ... branch:             ;

'/-             ; input -> int. (input -= '/')             ;
   -            ; counter - int(input) - 1                 ;
                ; Stack: [counter * 3, lookupPosition ]    ;
    1g          ; Get the char that we want to find        ;
      \1p\1g#   ; Overwrite the current char (not the old) ;

; Both branches: ;
,1+             ; Print the number and increment the counter ;

I wasn't able to get this version shorter, but this one is 44 bytes:

s #@~\3p:3g::'9`!\'/`*j;'/--3g#;:10g3p,1+:::

Thought I'd share it because of the neat trick with s - but storing the counter on the stack leads to that 1 char improvement

Justin

Posted 2017-04-07T13:17:16.727

Reputation: 19 757

2

Python 2, 59 bytes

s=''
for c in input():s+=c['/'<c<':':]or s[~int(c)]
print s

Try it online!

xnor

Posted 2017-04-07T13:17:16.727

Reputation: 115 687

2

APL (Dyalog Classic), 25 23 bytes

-2 bytes thanks to @FrownyFrog

((⊂⌷⊢)⍣≡⍳∘≢-11|⎕d∘⍳)⊃¨⊂

Try it online!

uses ⎕io←1

( below stands for an intermediate value in the evaluation)

⎕d is the string '0123456789'

⎕d⍳⍵ finds the (1-based in this case) indices of 's chars in ⎕d; for a non-digit the index is 11

11|⍵ is modulo - the 11s become 0s

≢⍵ is the length of

⍳≢⍵ is 1 2 ... till ≢⍵

so, (⍳≢⍵)-11|⎕d⍳⍵ gives us a vector i of the indices where we should look to get the resulting characters; however some of those indices may redirect to yet other (smaller) indices. To compute the transitive closure (i.e. the effective indices), we index the vector into itself (⊂⌷⊢, a train equivalent to (⊂i)⌷i or i[i]) and repeat that until it stabilises (⍣≡ is known as the fixed point operator).

finally we index into the original string: (...)⊃¨⊂

ngn

Posted 2017-04-07T13:17:16.727

Reputation: 11 449

How would it look as a train? – FrownyFrog – 2018-04-25T01:24:55.130

@FrownyFrog indeed, shorter – ngn – 2018-04-25T06:13:28.940

2

PHP 7.1 67 59 bytes

while(_&$c=$argn[$i++])$t.=($c^"0")<"
"?$t[~+$c]:$c;echo$t;

Takes input from STDIN; run as pipe with -nR or try it online.

  • _&$c=$s[$i++] loop through string (_&$c will result in something that is not "0"; so the only character that can break the loop is the empty string = end of input)
  • $c^"0" toggle bits 5 and 6 in the ascii code
  • <"\n" check if result is < chr(10)
  • if so, it is a digit: print previous character by index (and copy to current index)
  • else print this character

Thanks @Christoph for saving 12%

Titus

Posted 2017-04-07T13:17:16.727

Reputation: 13 814

1I know this is an old answer but: Negative string offsets ! (and that $s=$argn ... ?) for(;_&$c=$argn[$i++];)$t.=($c^"0")<"\n"?$t[~+$c]:$c;echo$t; – Christoph – 2018-04-23T12:13:34.050

2

Vim macro/keystrokes, 49 bytes

^M represent the return character (0x0A, 1 byte).

qqqqq/[0-9]^Myl:exe 'norm '.(@"+1).'h'^Mylnphx@qq@q

Explanation

qqq                                                     clear register q
   qq                                                   record into q
     /[0-9]^M                                           move the cursor to the next digit
             yl                                         yank the digit
               :exe 'norm '.(@"+1).'h'^M                move the cursor left that number of characters plus one
                                        yl              yank the char
                                          n             go back to the digit
                                           p            paste the char 
                                            hx          delete the digit
                                              @q        recursive call
                                                q       stop recording
                                                 @q     run the macro

TheFamilyFroot

Posted 2017-04-07T13:17:16.727

Reputation: 21

1

Python 2,83 80 bytes

r=input()
for i in r:
 if'/'<i<':':r=r.replace(i,r[r.find(i)+~int(i)],1)
print r

Try it online!

  • saved 3 bytes but checking ascii instead of is digit! Thanks to math_junkie!

Keerthana Prabhakaran

Posted 2017-04-07T13:17:16.727

Reputation: 759

1

Japt, 24 bytes

£Xn >J?U=UhYUgJ+Y-X):PÃU

Try it online!

Explanation:

£Xn >J?U=UhYUgJ+Y-X):PÃU
£                     Ã    Iterate through the input (implicit U) 
                             X becomes the iterative item, Y becomes the index
 Xn                          Try parseInt(X)
    >J                       > -1
                               In this case, this checks if X is a digit
      ?                      If true:
       U=                      Set U to 
         UhY                     U with the char at index Y set to:     
            UgJ+Y-X               The index at -1+Y-X
                   ):        Else:
                     P         variable P (just a no-op in this case)
                       U   Finally, return U

Oliver

Posted 2017-04-07T13:17:16.727

Reputation: 7 160

1

Ruby, 56 46 bytes

->s{i=0;s[i]=s[i+~s[i].to_i]while i=s=~/\d/;s}

Try it online!

Value Ink

Posted 2017-04-07T13:17:16.727

Reputation: 10 608

1

Python 2, 58 bytes

lambda s:reduce(lambda t,c:t+(c+t)['/'<c<':'and~int(c)],s)

This is essentially a port of my Jelly answer, plus the digit check from @xnor's Python answer.

Try it online!

Dennis

Posted 2017-04-07T13:17:16.727

Reputation: 196 637

1

Röda, 51 bytes

f a{x=0;a|{|y|a[x]=a[x+47-ord(y)]if[y=~`\d`];x++}_}

Try it online!

fergusq

Posted 2017-04-07T13:17:16.727

Reputation: 4 867

1

JavaScript ES6, 54 bytes

f=r=>[...r].reduce((a,s,i)=>a+(/\d/.test(s)?a[i+~s]:s))

f=r=>[...r].reduce((a,s,i)=>a+(/\d/.test(s)?a[i+~s]:s))

console.log(f("Prog2am0in6"));
console.log(f("abcd"));
console.log(f("a000"));
console.log(f("ban111"));
console.log(f("Hel0o W2r5d!"));
console.log(f("this 222a19e52"));
console.log(f("golfin5 3s24o0d4f3r3y3u"));
console.log(f("Prog2am0in6 Puz0les7&1Cod74G4lf"));
console.log(f("Replicants 4re3lik448ny3oth8r5mac6in8.8T64y'r371it9376a1b5n1fit7or2a1h2z17d."));

ogur

Posted 2017-04-07T13:17:16.727

Reputation: 111

1Welcome to PPCG! If you don't need your function name for recursive calls, unnamed functions are valid, so you can save two bytes on the f=. – Martin Ender – 2017-04-10T21:39:24.813

1

><> (Fish), 108 bytes (= 9 x 12 grid)

01-r>:0(\
"/"&::;?/
)?\v    \
":/v!?(":
")\ :>:"0
 !?\
${/  \ -1
&>\ ~{:&$
\ \ :"0"=
/\- 1}$/?
:v&//}~/~
 \o}\&$/ 

Try it here to see the fish swimming around.

  • Append -1 to the input stack then reverse the stack.
  • Loop: If top value is -1 then end (we've cycled through all characters). Otherwise:
  • Put the top character in the register; check to see whether it's in the range "0" to "9". If so:
    • rotate the stack the appropriate number of places
    • get the character being pointed to
    • rotate back and replace the number with the character from the register
  • Output; resume loop.

Théophile

Posted 2017-04-07T13:17:16.727

Reputation: 263

1

8086 machine code, 35 bytes

00000000  be 82 00 ac 98 50 2c 30  3c 09 77 0c 4e 89 f7 4e  |.....P,0<.w.N..N|
00000010  29 c6 58 ac aa 89 fe 50  5a b4 02 cd 21 80 fa 0d  |).X....PZ...!...|
00000020  75 e1 c3                                          |u..|
00000023

user5434231

Posted 2017-04-07T13:17:16.727

Reputation: 1 576

1

oK, 39 bytes

{x{x[y 0]:x@-/y}/(!#x),'0|{x*11>x}x-47}

Try it online!

zgrep

Posted 2017-04-07T13:17:16.727

Reputation: 1 291

1

J, 20 bytes

{~[:{~^:_#\-2+_1".,.

Try it online

                  ,.  Each character on a separate row
              _1".    Convert to numbers, replacing non-numbers with -1
                         (it becomes one row again)
            2+        Add 2.
         #\           Prefix lengths (range 1..length)
           -          Subtract
  [:{~^:_             Index into itself as long as it changes the result
{~                    Index into the original string

Credit to ngn for the inspiration.

22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |. The string backwards, because reduce is right-to-left.
            _1".[      The next character as a number (d), -1 if it's not a number,
                          and a space character produces an empty array.
         _1-           -1-d
      1{.              Take 1. If we have a nothing
                          at this point, that makes it a 0.
   ,                   Prepend the next character to the result of the previous call.
    {~                 Select the character. 0 is the first, _2 is second to last.
 ],                    Append the result.

In both solutions the version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

FrownyFrog

Posted 2017-04-07T13:17:16.727

Reputation: 3 112

1

Japt v2.0a0, 16 bytes

r\d@=hYUgY-°X¹gY

Try it


Explanation

                     :Implicit input of string U
r                    :Replace
 \d                  :  RegEx /\d/g
   @                 :  Pass each match X at index Y through a function
     hY              :    Set the character at index Y in U
       UgY-°X        :    To the character at index Y-++X
    =        ¹       :    Reassign to U
              gY     :    Get the character at index Y

Shaggy

Posted 2017-04-07T13:17:16.727

Reputation: 24 623

1

C# (.NET Core), 138 bytes

string d(string x){var e=x.ToCharArray();for(int i=0;++i<e.Length;){if(char.IsDigit(e[i]))e[i]=e[i-((e[i]-'0')+1)];}return new string(e);}

Try it online!

Knight King

Posted 2017-04-07T13:17:16.727

Reputation: 11

0

PHP, 79 Bytes

for(;++$i<strlen($a=&$argn);)is_numeric($l=$a[$i])?$a[$i]=$a[$i-$l-1]:0;echo$a;

Jörg Hülsermann

Posted 2017-04-07T13:17:16.727

Reputation: 13 026

0

PowerShell, 98 82 76 71 bytes

switch -r(($n=($args|% t*y))){\d{$n[$j]=$n[$j-$_+47]}.{$j++}};$n-join''

Try it online!

Suggestions welcome!

EDIT It looks like powershell will redefine $i when I set it on the right side of the equation in the if statement. I did not realize it looked at that first - saved 1 byte

EDIT 2 Found another way to find the current index by using $j as an index that iterates for each foreach pass - saved 15 bytes and copied method from @AdmBorkBork for creating character arrays [found here][2]! - saved another 6 bytes

EDIT 3 Changed to a switch statement and collapsed unnecessary spaces in the output

Sinusoid

Posted 2017-04-07T13:17:16.727

Reputation: 451

0

REXX, 118 bytes

a=arg(1)
do n=1 to length(a)
  c=substr(a,n,1)
  if datatype(c)=num then do
    a=overlay(substr(a,n-1-c,1),a,n)
    end
  end
say a

idrougge

Posted 2017-04-07T13:17:16.727

Reputation: 641

0

Currently 4 JavaScript solutions

x=>[...x].map((p,i,a)=>a[i]=1+p>9?a[i-1-p]:p).join``
r=>[...r].reduce((a,s,i)=>a+(1+s>9?a[i+~s]:s))
s=>s.replace(a=/\d/g,(c,i)=>a[i]=a[i+=~c]||s[i])
x=>f=x!=(x=x.replace(/\d/,(m,o)=>x[o+~m]))?f():x

l4m2

Posted 2017-04-07T13:17:16.727

Reputation: 5 985

0

R, 105 bytes

function(S,s=el(strsplit(S,"")))for(k in 1:nchar(S))cat(s[k]<-"if"(s[k]%in%0:9,s[k-strtoi(s[k])-1],s[k]))

Try it online!

Giuseppe

Posted 2017-04-07T13:17:16.727

Reputation: 21 077