11
\$\begingroup\$

This challenge is the first in a two-challenge series about Repetition. The second will be up soon.

In a language called Repetition (something I just made up), there consists an infinite string of 12345678901234567890..., with 1234567890 repeating forever.

The following syntax is available to output numbers:

  • +-*/: This inserts the operator into the string of repeating digits.
    • Examples:
      • + -> 1+2 = 3 (The + inserts a + between 1 and 2)
      • +* -> 1+2*3 = 1+6 = 7 (Same as above, except two operators are used now)
      • / -> 1/2 = 0 (Repetition uses integer division)
      • // -> 1/2/3 = 0/3 = 0 (Repetition uses "left association" with multiple subtractions and divisions)
    • Each operator is inserted so that it has one digit to its left, unless there are c's (see below).
  • c: Concatenates with the next digit in the string.
    • Examples:
      • c+ -> 12+3 = 15 (The c "continues" the 1 and concatenates it with the next digit, 2, to form 12)
      • +c -> 1+23 = 24
      • ccc -> 1234
  • (): Brackets for processing numbers.
    • Examples:
      • (c+)* -> (12+3)*4 = 15*4 = 60 (Repetition uses the order of operations)
      • (c+)/c -> (12+3)/45 = 15/45 = 0
      • (cc+c)/ -> (123+45)/6 = 168/6 = 28
  • s: Skip a number (removes the number from the infinite string).
    • s+ -> 2+3 = 5 (s skips 1)
    • csc -> 124 (The first c concats 1 and 2, the s skips 3, and the final c concats 12 to 4)
    • +s+ -> 7 (The first + adds 1 and 2 to make 3, s skips 3, and the final + adds 3 to 4 to make 7)
    • cs*(++) -> 12*(4+5+6) = 12*15 = 180

In the examples above, only a finite amount of digits in the infinite string are used. The number of digits used is equivalent to number of operators, concats and skips + 1.

Your task is, when given a string of Repetition code, output the result.

Examples of input and output are:

++ -> 6
- -> -1
(-)* -> -3
cscc -> 1245
(cc+c)/ -> 28
cc+c/ -> 130
cs*(++) -> 180

This is code golf, so shortest code in bytes wins!

Specs:

  • You are guaranteed that the result will never go above 2^31-1.
  • You are also guaranteed that the input will only consist of the symbols +-*/cs().
  • An empty program will output 1.
\$\endgroup\$
  • \$\begingroup\$ What about the ~s? Don't leave us hanging. \$\endgroup\$ – Robert Fraser Sep 17 '16 at 8:24
  • \$\begingroup\$ @RobertFraser Whoops, that was a mistake - c was originally ~, but it appears that I haven't fixed that completely. \$\endgroup\$ – clismique Sep 17 '16 at 8:29
  • 1
    \$\begingroup\$ @TonHospel Ooh, you've got a good point there. The "s" symbol essentially removes the number it's associated with from the infinite string altogether, so it's a yes for both scenarios. \$\endgroup\$ – clismique Sep 17 '16 at 9:29
  • 2
    \$\begingroup\$ Your specification however says s+ is 2+3 as its first example. And still s keeps bugging me. I wonder how +s()+ expands. If it is 1+(2)+4 then ( comes before 2 but the s that comes even before the ( seemingly still skips 3, not 2. If however the result is 1+(3)+4 then the effect of a s depends on what comes after it (compare it with +s+) \$\endgroup\$ – Ton Hospel Sep 17 '16 at 18:23
  • 1
    \$\begingroup\$ sc is 23 and s+ is 1+3? Does s skip the 1 now or the 2? All examples use the first operation on operands 1 and 2 ... so sc should be 13. \$\endgroup\$ – Titus Sep 17 '16 at 19:14
4
\$\begingroup\$

JavaScript (ES6), 110 bytes

s=>eval((" "+s)[R='replace'](/[^\)](?!\()/g,x=>x+i++%10,i=1)[R](/c| (\ds)+|s\d/g,"")[R](/\d+\/\d+/g,"($&|0)"))

Very simple, but integer division adds 25 bytes. For some reason a regex in JS can't match both the beginning of a string and the first character, so that adds a few bytes as well.

How it works

  1. Prepend a space to the input.
  2. Append the next digit to each character (except )) that isn't immediately before a (.
  3. Remove each c, a digit + s at the beginning (1s2 -> 2), and each s + a digit (3s4 -> 3).
  4. Turn each division operation into int-division (1/2 -> (1/2|0)).
  5. Evaluate and return.
\$\endgroup\$
  • \$\begingroup\$ OK... your program has a bug... ss+ returns 6, when it's meant to return 7 (The two s's skip 1 and 2, so the + adds 3 and 4). \$\endgroup\$ – clismique Sep 17 '16 at 23:34
  • \$\begingroup\$ @Qwerp-Derp Thanks, fixed. \$\endgroup\$ – ETHproductions Sep 18 '16 at 14:10
  • \$\begingroup\$ Something like /^|,|$/g will only match once at the beginning because both matches would have the same index. $ doesn't have the same problem because the match has a greater index than any other possible match. \$\endgroup\$ – Neil Sep 18 '16 at 19:15
0
\$\begingroup\$

Batch, 332 bytes

@echo off
set s=
set e=
set d=
set r=
set/ps=
:d
set/ad=-~d%%10
:l
if "%s%"=="" goto g
set c=%s:~0,1%
set s=%s:~1%
if %c%==( set e=%e%(&goto l
if %c%==) set r=%r%)&goto l
if %c%==s goto d
if %c%==c set c=
if "%r%"=="" set/ar=d,d=-~d%%10
set e=%e%%r%%c%
set/ar=d
goto d
:g
if "%r%"=="" set/ar=d
cmd/cset/a%e%%r%

The behaviour of s makes this very awkward. (Maybe cs should evaluate to 13 and -s to -2?) Variables:

  • s input string (explicitly blanked out because set/p doesn't change the variable if you don't input anything)
  • e partial expression in normal integer arithmetic (which we can pass to set/a as a form of eval)
  • d next digit from the infinite string of digits
  • r right hand side of latest operator. We can't concatenate this immediately because ( needs to come first, but we need to store it so that s won't increment it. Fortunately it does end up making the handling of ) slightly easier.
  • c current character.
\$\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.