7
\$\begingroup\$

PlusOrMinus is an esoteric programming language invented by Esolang user PythonshellDebugwindow.

Language specification

There is a wrapping byte accumulator (incrementing 255 results in 0 and decrementing 0 results in 255) in the language. The initial valus of the accumulator is 0.

There are two instructions in the language:

  • +, which increments the accumulator by one.
  • -, which prints the accumulator as ASCII (e.g.: A if accumulator is 65), then decrements the accumulator by one.

Characters that are neither + nor - are ignored.

Examples

Here are some examples to test your programs:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++-++++++++-+-++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++-++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-

Prints Hello, World!.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++-++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-

Prints Nope..

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------

Prints ZYXWVUTSRQPONMLKJIHGFEDCBA.

blah

Prints nothing.

+++++++++++++++++++++++++++++++++?-

Prints !.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++

Prints A.

++++++++++++++++++++++++++++++++++--

Prints "!.

Goal

This is , so your goal is to use the fewest bytes to interpret the language. The program to interpret can be from a console input, file input or string.

\$\endgroup\$
22
  • 1
    \$\begingroup\$ @pacman256 I did read both challenges, that's why I linked it. \$\endgroup\$
    – Wheat Wizard
    Commented Apr 25 at 12:51
  • 5
    \$\begingroup\$ You can’t print as ASCII if the number is above 127, and it is dubious for numbers below 32. How should those cases be handled? \$\endgroup\$
    – Luis Mendo
    Commented Apr 25 at 16:35
  • 3
    \$\begingroup\$ Is input guaranteed to only contain - and + or should other characters do nothing? \$\endgroup\$
    – Tbw
    Commented Apr 25 at 20:09
  • 2
    \$\begingroup\$ I think you should clarify in the question that we have to support characters other than + and - in the input string (if that's really what you want). At present, this appears clearly as part of the specification of the original language, but not as clearly as a requirement for the challenge. \$\endgroup\$
    – Arnauld
    Commented Apr 26 at 22:18
  • 3
    \$\begingroup\$ Suggested test case (currently failing in accepted answer) ++++++++++++++++++++++++++++++++++-- should output "!. \$\endgroup\$ Commented May 21 at 11:40

29 Answers 29

7
\$\begingroup\$

AWK -vRS=(.), 37 bytes

RT~/+/{x++}RT~/-/{printf"%c",x--%256}

Attempt This Online!

Uses a regex as a record seperator to split the input into seperate characters and matches them one-by-one.

\$\endgroup\$
2
  • \$\begingroup\$ Works with the provided weak test cases, but try “+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++”. For this should print “A”, but your code prints ”AB”. \$\endgroup\$
    – manatwork
    Commented Apr 25 at 23:53
  • \$\begingroup\$ Thanks. The " " did something totally different from what I expected. Should be fixed now. \$\endgroup\$
    – corvus_192
    Commented Apr 26 at 10:16
5
\$\begingroup\$

Perl 5 + -p055, 24 bytes

-2 bytes thanks to @Neil!

$-+=y/+//;$_=chr$---%256

Try it online!

Explanation

Cheesing a little on the args, using -055 to split input on - and using y/+// to count the +s (ignoring other chars, thanks @Johnathan Allan!).

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Updated. Thanks @JonathanAllan! \$\endgroup\$ Commented Apr 26 at 17:35
  • 1
    \$\begingroup\$ 24 bytes. \$\endgroup\$
    – Neil
    Commented Apr 26 at 23:49
  • \$\begingroup\$ Thanks @Neil! I'm so certain I tried that first and hit problems with it but must've done something else! 😅 \$\endgroup\$ Commented Apr 27 at 9:41
4
\$\begingroup\$

Pip, 29 25

C{i+:Dai+1%:256}M#*H:a^'-

- 4 bytes thanks to DLosc

Attempt This Online!

C{i+:Dai+1%:256}M#*H:a^'-­⁡​‎⁠⁠‎⁡⁠⁢⁢⁢‏⁠‎⁡⁠⁢⁢⁣‏⁠‎⁡⁠⁢⁢⁤‏⁠‎⁡⁠⁢⁣⁡‏⁠‎⁡⁠⁢⁣⁢‏⁠‎⁡⁠⁢⁣⁣‏⁠‎⁡⁠⁢⁣⁤‏⁠‎⁡⁠⁢⁤⁡‏‏​⁡⁠⁡‌⁢​‎⁠‎⁡⁠⁢‏⁠⁠⁠⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏⁠‎⁡⁠⁢⁡⁢‏⁠‎⁡⁠⁢⁡⁣‏⁠‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏⁠⁠⁠‏​⁡⁠⁡‌⁣​‎⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‏​⁡⁠⁡‌⁤​‎⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠⁠⁠‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌­
                     a^'-  # ‎⁡Split input by "-"
 {             }M#*H:      # ‎⁢Map through lengths of all but last item as a...
  i+:Da                    # ‎⁣ Increment i (initialised to 0) by length of a - 1
       i+1%:256            # ‎⁤ Return i+1 mod 256
C                          # ‎⁢⁡Convert codepoints to ASCII
💎

Created with the help of Luminespire.

\$\endgroup\$
3
  • \$\begingroup\$ Here's 26 bytes. \$\endgroup\$
    – DLosc
    Commented Apr 25 at 21:55
  • \$\begingroup\$ 25 by rearranging so the M and the H aren't next to each other. \$\endgroup\$
    – DLosc
    Commented Apr 25 at 22:01
  • 3
    \$\begingroup\$ +++++++++++++++++++++++++++++++++?- outputs " rather than the expected ! (not handling the requirement that non-+- should be ignored). \$\endgroup\$ Commented Apr 26 at 17:02
4
\$\begingroup\$

JavaScript (Node.js), 57 bytes

Expects an array of characters and returns a string.

s=>Buffer(s.flatMap(c=>c=="-"?a--:(a+=c=="+",[]),a=0))+""

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ why was this answer accepted i am working on a 10 byter in a golflang \$\endgroup\$
    – pacman256
    Commented Apr 25 at 12:52
  • \$\begingroup\$ You didn't post it yet. \$\endgroup\$
    – None1
    Commented Apr 25 at 12:55
  • \$\begingroup\$ Fail -+, expected \x00, get \x00\x00 \$\endgroup\$
    – l4m2
    Commented Apr 26 at 0:02
  • \$\begingroup\$ @l4m2 Thanks for reporting this. Fixed. \$\endgroup\$
    – Arnauld
    Commented Apr 26 at 5:51
  • 3
    \$\begingroup\$ 49 byte version when given +++++++++++++++++++++++++++++++++?- outputs two null bytes rather than the expected ! (not handling the requirement that non-+- should be ignored). \$\endgroup\$ Commented Apr 26 at 17:08
3
\$\begingroup\$

Funge-98, 47 bytes

>~#<:'+w$:'U3*-3jc+1_$0
^#:w-' <j7-1_$'U3*4j6:,

Try it online!

The program never halts, so if you run it using TIO, you have to manually cancel execution in order to view the output.

\$\endgroup\$
0
3
\$\begingroup\$

Uiua 0.11.0, 26 bytes SBCS

▽:+@\0◿256\+-⬚0↻¯1,∩=@+,@-

Try on Uiua Pad!

Takes a string of + and - and returns the output string. If the input is guaranteed to only contain +-, we can save 2 bytes by replacing ∩=@+,@- with ¬.=@- (and probably more, with a completely different strategy).

Explanation

@-    # push literal character - to stack
@+,   # dup input to top and push +
∩=    # get two arrays with 1s where + and - are
,     # dup the - array to top
⬚0↻¯1 # shift 1 to the left, filling with 0 at front
-     # subtract from the + array
\+    # plus scan: cumulative sums
◿256  # mod 256
+@\0  # add literal character null, converts to ASCII
▽:    # flip the - array to top and apply mask
\$\endgroup\$
3
\$\begingroup\$

Perl 5 -p, 35 bytes

s/.*?-/$x+=$&=~y-+--;chr$x--%256/ge

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Nice use of string repetition, I came up with a very similar approach originally but didn't think to use that! (Went with cheesing -055F in the end!) \$\endgroup\$ Commented Apr 25 at 13:24
  • 1
    \$\begingroup\$ I was previously working with s/(\+)|-/$1?($x++,""):chr$x--%256/ge and also s/(\+*)-/$x+=length$1;chr$x--%256/ge with 36 bytes \$\endgroup\$ Commented Apr 25 at 13:27
  • 1
    \$\begingroup\$ @DomHastings, saved 2 bytes without using "repetition" operator but "not", and changing post-increment to pre-increment to prevent having !0-> 1 \$\endgroup\$ Commented Apr 25 at 13:51
  • \$\begingroup\$ oops i accidentally downvoted for some reason and it locked me into that, if you edit it ill upvote mb \$\endgroup\$
    – pacman256
    Commented Apr 27 at 17:45
  • 1
    \$\begingroup\$ @JonathanAllan here is a fix s/.*?-/$x+=$&=~y-+--;chr$x--%256/ge but now there's a shorter perl5 solution \$\endgroup\$ Commented Apr 30 at 11:59
3
\$\begingroup\$

05AB1E, 18 15 14 bytes

'-¡'+QO<ηO>₁%ç

-3 bytes thanks to @IvanG.
-1 byte porting @JonathanAllan's Jelly answer

I/O as a list of characters.

Try it online or verify all test cases.

Explanation:

'-¡           '# Split the (implicit) input-list of characters on "-"
   '+Q        '# Check for each inner character whether it equals "+"
      O        # Sum each inner list of 1s and 0s together
       <       # Decrease each sum by 1
        η      # Get the prefixes of this list of sums-1
         O     # Sum each prefix
          >    # Then increase each prefix-sum by 1
           ₁%  # Modulo-256
             ç # Convert each integer to a character with this codepoint
               # (after which the list of characters is output implicitly as result)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ -2 bytes: S'-¡'+QOηOā<-₁%ç \$\endgroup\$
    – Ivan G.
    Commented May 12 at 17:09
  • \$\begingroup\$ @IvanG. Thanks! :) \$\endgroup\$ Commented May 12 at 18:09
3
\$\begingroup\$

Vyxal 3, 14 bytes

'-sᵛ'+Cv@₇%ꜝOṪ

Try it Online!

would be 14 if vyxal didnt automatically treat + as a regex symbol

Edit-- Bug was fixed

\$\endgroup\$
10
  • \$\begingroup\$ You need to use s instead of ÷, as ÷ doesn't keep empty splits \$\endgroup\$
    – lyxal
    Commented Apr 25 at 13:20
  • \$\begingroup\$ no clue why. it just be like that \$\endgroup\$
    – lyxal
    Commented Apr 25 at 13:22
  • 1
    \$\begingroup\$ let me uh go fix that rq \$\endgroup\$
    – lyxal
    Commented Apr 25 at 13:23
  • 2
    \$\begingroup\$ The fix should be live on vyxal.github.io in a few minutes \$\endgroup\$
    – lyxal
    Commented Apr 25 at 13:33
  • 1
    \$\begingroup\$ Inputting a single string of - characters does not seem to work - it only prints one character, and it doesn't appear to even necessarily be one of the ones that should be printed. EDIT: just does not appear to deal with consecutive - properly e.g. ++++++++++++++++++++++++++++++++++-- should output "! but it only outputs ". \$\endgroup\$ Commented May 21 at 11:33
2
\$\begingroup\$

Jelly, 14 bytes

ṣ”-Ṗċ€”+’Ä‘%⁹Ọ

A monadic Link that accepts a list of characters, or a full program that accepts a string argument.

Try it online! Or see the test-suite.

How?

ṣ”-Ṗċ€”+’Ä‘%⁹Ọ - Link: list of characters, Code      e.g. "---+++++--"
ṣ”-            - split {Code} at '-' characters           ["", "", "", "+++++", "", ""]
   Ṗ           - remove the (possibly empty) rightmost    ["", "", "", "+++++", ""]
    ċ€”+       - count the '+' characters in each         [0,    0,   0, 5,  0]
        ’      - decrement all values                     [-1,  -1,  -1, 4, -1]
         Ä     - cumulative sums                          [-1,  -2,  -3, 1 , 0]
          ‘    - increment all values                     [ 0,  -1,  -2, 2,  1]
           %⁹  - mod 256 all values                       [ 0, 255, 254, 2,  1]
             Ọ - convert all to characters                "\x00ÿþ\x02\x01"
               - (if running as a full program, implicitly prints to stdout)
\$\endgroup\$
2
\$\begingroup\$

Google Sheets, 93 bytes

=SORT(JOIN(,IFNA(CHAR(SCAN(,SPLIT(A1,"-",,),LAMBDA(a,c,MOD(a+LEN(c),256)))-COLUMN(A:ZZ)+1))))
\$\endgroup\$
2
\$\begingroup\$

Lua, 149b

Read a character from standard in, and look it up in a table of two functions - call empty function otherwise.

a=0;while 1 do(({["+"]=function()a=(a+1)%256;end,["-"]=function()io.write(string.char(a))a=a<1 and 255 or a-1 end})[io.read(1)]or function()end)()end

Lua, 109b

Read character from stdin, convert to an increment, decrement, or zero, from a table. If we're going to decrement, convert accumulator number to char and print it out before adding.

a=0;while 1 do c=({["+"]=1,["-"]=-1})[io.read(1)]or 0;io.write(c<0 and string.char(a)or"")a=(a+c+256)%256;end

\$\endgroup\$
4
  • \$\begingroup\$ Welcome to CGCC! I don't know Lua but for your interest, there is tips for golfing in Lua. Also Lua is available at Try it online! (The stdout is not printed to screen until timeout at 60s). \$\endgroup\$ Commented Apr 26 at 19:35
  • \$\begingroup\$ @JonathanAllan Thank you! Long time lurker, wanted to see what I could pull off. The regular Lua shell on my machine prints it out straight away, maybe it just hangs waiting for the next character there since it does not handle EOF \$\endgroup\$
    – VBW
    Commented Apr 26 at 21:18
  • \$\begingroup\$ @VBW Hi! TIO is non-interactive, so you can't see thr output before the program terminates. Though I don't know Lua very much, it seems that you program never terminates. but a regular shell doesn't have this restriction. \$\endgroup\$
    – None1
    Commented Apr 27 at 1:15
  • \$\begingroup\$ Yes, I don't see it as a problem that you need to wait on TIO I was just letting you know. \$\endgroup\$ Commented Apr 27 at 14:18
2
\$\begingroup\$

Charcoal, 19 bytes

⭆⌕AΦS№+-ι-℅﹪⁻ι⊗κ²⁵⁶

Try it online! Link is to verbose version of code. Explanation:

    S               Input string
   Φ                Filtered where
      +-            Literal string `+-`
     №              Contains
        ι           Current character
 ⌕A                 Find all indices of
         -          Literal string `-`
⭆                   Map over indices and join
             ι      Index of current `-`
            ⁻       Minus
               κ    Count of previous `-`s
              ⊗     Doubled
           ﹪        Modulo
                ²⁵⁶ Literal integer `256`
          ℅         Convert to character
                    Implicitly print
\$\endgroup\$
0
2
\$\begingroup\$

Scala 3, 123 bytes

A port of @none1's JavaScript answer in Scala.


Golfed version. Attempt This Online!

p=>{var o="";var x=0;for(i<-p){i match{case'+'=>x+=1;if(x==256)x=0 case'-'=>o+=x.toChar;x-=1;if(x== -1)x=255;case _ =>}};o}

Ungolfed version. Attempt This Online!

object Main {
  def plusOrMinus(program: String): String = {
    var output = ""
    var x = 0
    for (i <- program) {
      i match {
        case '+' =>
          x += 1
          if (x == 256) x = 0
        case '-' =>
          output += x.toChar
          x -= 1
          if (x == -1) x = 255
        case _ => // handle other characters if necessary
      }
    }
    output
  }

  def main(args: Array[String]): Unit = {
    val input = 
s"""++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++-++++++++-+-++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++-++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-"""


    println(plusOrMinus(input))
  }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @JonathanAllan I have fixed it. \$\endgroup\$
    – 138 Aspen
    Commented Apr 27 at 2:51
2
\$\begingroup\$

Stax, 15 bytes

Ç¿₧Δ─Θî²1%≤{‼aù

Run and debug it

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

JavaScript, 125 111 109 Bytes

r=>{f="",u=0;for(n of r)"+"==n?256==++u&&(u=0):"-"==n&&(f+=String.fromCharCode(u),-1==--u)&&(u=255);return f}

Ungolfed version:

function plusorminus(program){
    var output='',x=0;
    for(let i of program){
        if(i=='+'){
            x++;if(x==256){x=0}
        }else if(i=='-'){
            output+=String.fromCharCode(x);x--;if(x==-1){x=255}
        }
    }
    return output
}

You can run the code in this snippet:

y=r=>{f="",u=0;for(n of r)"+"==n?256==++u&&(u=0):"-"==n&&(f+=String.fromCharCode(u),-1==--u)&&(u=255);return f}
console.log(y('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++-++++++++-+-++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++-++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-'))
console.log(y('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++-++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-'))
console.log(y('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------'))
console.log(y('blah'))
console.log(y('+++++++++++++++++++++++++++++++++?-'))
console.log(y('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-++'))
console.log(y('++++++++++++++++++++++++++++++++++--'))

\$\endgroup\$
2
  • \$\begingroup\$ A few tips: 1. you can use a lambda/arrow function, 2. don't use var, ever 3. read codegolf.stackexchange.com/questions/2682/…, 4. have fun! \$\endgroup\$
    – corvus_192
    Commented Apr 25 at 19:20
  • \$\begingroup\$ Thanks for your advice! \$\endgroup\$
    – None1
    Commented Apr 26 at 10:05
1
\$\begingroup\$

Python, 100 Bytes

lambda x:''.join(chr(x[:i].count('+')-x[:i].count('-')&255)if x[i]=='-'else''for i in range(len(x)))

Both the argument and the return value are strings.

Explanation: + and other characters don't produce any output, the value that a - outputs equals to the number of + minus the number of - before it modulo 256.

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 87 bytes using an enumerate and moving to Python 3.8+ (to use the walrus operator, :=) \$\endgroup\$ Commented Apr 27 at 16:23
  • \$\begingroup\$ ...or 70 bytes using the same approach, tracking the sum as you go. \$\endgroup\$ Commented Apr 27 at 16:41
1
\$\begingroup\$

C (gcc), 49 47 46 bytes

c;f(char*s){for(;*s;)*s++%5?c++:putchar(c--);}

Pretty straightforward.

-1 byte thanks to Arnauld.

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Does anybody know if it's possible to save bytes with this trick? \$\endgroup\$
    – Blue
    Commented Apr 25 at 15:48
  • 1
    \$\begingroup\$ You can use s++%5 instead of s++-45. \$\endgroup\$
    – Arnauld
    Commented Apr 26 at 16:06
  • 1
    \$\begingroup\$ +++++++++++++++++++++++++++++++++?- outputs " rather than the expected ! (not handling the requirement that non-+- should be ignored). \$\endgroup\$ Commented Apr 26 at 17:19
  • \$\begingroup\$ I tried to fix the problem but my edit was rejected for I didn't "strive to preserve the goals of the post's owner.", but I did \$\endgroup\$
    – None1
    Commented May 6 at 23:28
  • \$\begingroup\$ 42 bytes \$\endgroup\$
    – jdt
    Commented Sep 14 at 1:45
1
\$\begingroup\$

Pascal, 158 B

The implementation‑defined set of char values must correspond to ASCII and the ordinal value of the implementation‑defined constant maxChar must be greater than or equal to 255.

program P(input,output);var C:char;A:0‥255;begin
A≔0;while not EOF do begin
read(C);if C='−' then write(chr(A));A≔(A+ord(C='+')−ord(C='−'))mod 256 end end.

−3 B: If you have a processor supporting “Extended Pascal” as defined by ISO standard 10206, you may write a zero‑width string:

program P(input,output);var C:char;A:0‥255;begin
A≔0;while not EOF do begin
read(C);write(chr(A):ord(C='−'));A≔(A+ord(C='+')−ord(C='−'))mod 256 end end.

Users of the FreePascal Compiler need to insert a {$modeSwitch ISOMod+} compiler directive. This ensures an ISO‑compliant definition of the mod operator, in particular in case of negative dividends the result is still non‑negative (Euclidean‑like definition of mod).

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

Python 3, 66 bytes

v=0
for c in input():v%=256;v+=c=='+';v-='-'==c!=print(end=chr(v))

A full program that reads from stdin and prints to stdout.

Try it online! Or see the test-suite.

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

YASEPL, 76 75 59 bytes

=a=i=1'=l®1`1!r¥i,1}7,plus,2!+`2}7,minus,3!›-`3%256!i+}2,l
\$\endgroup\$
1
\$\begingroup\$

brainfuck, 65 57 bytes

>,[>--[++++++<->]+<[--[[-]>-<]>[<<.->>-][-]<]>[-<<+>>]<,]

Try it online!

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

Kotlin, 76 bytes

{fold(0){a,c->when(c){'-'->{print(a.toChar());a-1}'+'->a+1 else->a}and 255}}

Try it online!

-1 byte thanks to ceilingcat.

\$\endgroup\$
1
  • \$\begingroup\$ @ceilingcat I think you have to account for negative values. \$\endgroup\$
    – Cactusroot
    Commented Oct 28 at 7:27
0
\$\begingroup\$

C#, 102 100 bytes

-2 thanks to @ceilingcat's suggestion to replace '+' and '-' with their ASCII codes (43 and 45)

void I(string s){byte b=0;foreach(var c in s)System.Console.Write((char)(c==43?++b-b:c==45?b--:0));}

Try it online!

Explanation:

void InterpretPlusOrMinus(string input)
{
    byte b = 0; // initialize accumulator 'b'

    foreach (var c in input)
    {
        System.Console.Write((char)( // Print the ASCII representation of:
            (c == 43)?         // If c is '+':
                 ++b - b :      //    ++b-b (increment b and return 0)
                 (c == 45)?    // Else If c is '-':
                     b-- :      //    b--
                     0          // Else: 0
        ));
    }
}

This code works in .NET 5+

\$\endgroup\$
1
  • \$\begingroup\$ Suggest 43 for '+' and 45 for '-' \$\endgroup\$
    – ceilingcat
    Commented May 11 at 6:30
0
\$\begingroup\$

PowerShell Core, 41 bytes

switch($args){+{$c++}-{[char]($c--%256)}}

Try it online!

Function that takes a string using splatting
Returns an array of characters

\$\endgroup\$
0
\$\begingroup\$

J, 37 bytes

[:(]#a.{~256|[:+/\[-_1|.!.0])/'+-'=/]

Shameless port of Tbw's amazing Uiua answer.

Attempt This Online!

[:(]#a.{~256|[:+/\[-_1|.!.0])/'+-'=/]
                                    ]   NB. input
                              '+-'      NB. string literal of ops
                                  =/    NB. equality table of each op and input
[:                                      NB. cap forces f(g(x)) instead of a hook
  (                         )/          NB. fold, the two table rows become left/right arg
                           ]            NB. list of chars equal to -
                    _1|.!.0             NB. shift right 1 and fill with 0
                  [                     NB. list of chars equal to +
                   -                    NB. subtract
             [:+/\                      NB. prefix sums (sum-reduce(+/) on all prefixes)
         256|                           NB. mod 256
     a.{~                               NB. index into the ascii alphabet
   ]                                    NB. list of chars equal to -
    #                                   NB. copy with boolean vector acts as a filter
\$\endgroup\$
0
\$\begingroup\$

Japt -hP, 20 bytes

q-
ÆËo+ ÅÊÃåÄ ËuG² d

Try it

q-\nÆËo+ ÅÊÃåÄ ËuG² d     :Implicit input of string U
q-                        :Split on "-"
  \n                      :Reassign to U
    Æ                     :Modify last element
     Ë                    :  Map remaining elements in U
      o+                  :    Keep only "+"s
         Å                :    Slice off first character
          Ê               :    Length
           Ã              :  End map
            å             :  Cumulatively reduce by
             Ä            :    Addition, after prepending a 1
               Ë          :  Map
                u         :    Modulo
                 G        :      16
                  ²       :      Squared
                    d     :    Character at that codepoint
                          :Implicit output of last element joined to a string
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 59 bytes

char i;f(char*c){i+=*c==43;*c==45&&putchar(i--);f(c+!!*c);}

Ungolfed:

char i; // the counter
/*implicit int here */ f (char* c) {
    // increment the counter if *c is '+' (43), since == returns 1 for true
    i += *c == 43;
    // if c is not '-' (45), the condition will short circuit, otherwise putchar will run
    *c == 45 && putchar(i--);
    // if *c is the end of the string (0), !!*c will be 0, 
    // and the function will enter an infinite loop that does nothing, 
    // otherwise we will call f with a ptr to the next character.
    f(c + !!*c);
}

This handles non +/- characters properly, unlike the other C answer, however it does either hang indefinitely or overflow the stack (fixing this costs a byte). This also requires the -funsigned-char flag to run properly.

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ @emanresuA your code doesn't handle non +/- characters properly, if you try adding a ? to the start of the string, it breaks completely \$\endgroup\$
    – ayaan098
    Commented Oct 31 at 19:26
  • \$\begingroup\$ ... actually I'm dumb and missed the wrapping requirement that the char i enables, but this 58 still works \$\endgroup\$
    – emanresu A
    Commented Oct 31 at 19:29
0
\$\begingroup\$

BrainChild, 97 bytes

function(int l,@int s){int a=0;int i=0;while(i<l){if(s[i]==43)a++if(s[i++]==45)putchar(a--%256)}}

Takes input as a char array

Readable

function PlusMinus(int length, @int chars){
    int acc=0;
    int i=0;
    while(i<length){
        if(chars[i]=='+')
            acc++
        if(chars[i++]=='-')
            putchar((acc--)%256)
    }
}

Try It Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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