Skip to main content
removed the original version / added the new test cases
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (Node.js), 57 bytes

A less interesting version with input validationExpects an array of characters and returns a string.

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

Try it online!


JavaScript (Node.js), 49 bytes

Expects an array of characters and returns a string.

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

Try it online!Try it online!

Commented

s =>             // s[] = input array
Buffer(          // turn into a Buffer:
  s.flatMap(c => //   for each character c in s[]:
    a > (        //     compare the current value of the accumulator
      a -=       //     with the updated value obtained by subtracting
        c + 1    //     either +1 or -1, according to c
    ) ?          //     if the accumulator was decremented (c = '+'):
      []         //       force flatMap() to ignore this entry
    :            //     else (c = '-'):
      1 - a      //       return -(accumulator - 1)
                 //       (with implicit modulo 256)
    a = 0        //     start with accumulator = 0
  )              //   end of flatMap()
) + ""           // end of Buffer(); coerce it to a string

JavaScript (Node.js), 57 bytes

A less interesting version with input validation.

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

Try it online!


JavaScript (Node.js), 49 bytes

Expects an array of characters and returns a string.

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

Try it online!

Commented

s =>             // s[] = input array
Buffer(          // turn into a Buffer:
  s.flatMap(c => //   for each character c in s[]:
    a > (        //     compare the current value of the accumulator
      a -=       //     with the updated value obtained by subtracting
        c + 1    //     either +1 or -1, according to c
    ) ?          //     if the accumulator was decremented (c = '+'):
      []         //       force flatMap() to ignore this entry
    :            //     else (c = '-'):
      1 - a      //       return -(accumulator - 1)
                 //       (with implicit modulo 256)
    a = 0        //     start with accumulator = 0
  )              //   end of flatMap()
) + ""           // end of Buffer(); coerce it to a string

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!

added another version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (Node.js), 57 bytes

A less interesting version with input validation.

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

Try it online!


JavaScript (Node.js), 49 bytes

Expects an array of characters and returns a string.

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

Try it online!

Commented

s =>             // s[] = input array
Buffer(          // turn into a Buffer:
  s.flatMap(c => //   for each character c in s[]:
    a > (        //     compare the current value of the accumulator
      a -=       //     with the updated value obtained by subtracting
        c + 1    //     either +1 or -1, according to c
    ) ?          //     if the accumulator was decremented (c = '+'):
      []         //       force flatMap() to ignore this entry
    :            //     else (c = '-'):
      1 - a      //       return -(accumulator - 1)
                 //       (with implicit modulo 256)
    a = 0        //     start with accumulator = 0
  )              //   end of flatMap()
) + ""           // end of Buffer(); coerce it to a string

JavaScript (Node.js), 49 bytes

Expects an array of characters and returns a string.

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

Try it online!

Commented

s =>             // s[] = input array
Buffer(          // turn into a Buffer:
  s.flatMap(c => //   for each character c in s[]:
    a > (        //     compare the current value of the accumulator
      a -=       //     with the updated value obtained by subtracting
        c + 1    //     either +1 or -1, according to c
    ) ?          //     if the accumulator was decremented (c = '+'):
      []         //       force flatMap() to ignore this entry
    :            //     else (c = '-'):
      1 - a      //       return -(accumulator - 1)
                 //       (with implicit modulo 256)
    a = 0        //     start with accumulator = 0
  )              //   end of flatMap()
) + ""           // end of Buffer(); coerce it to a string

JavaScript (Node.js), 57 bytes

A less interesting version with input validation.

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

Try it online!


JavaScript (Node.js), 49 bytes

Expects an array of characters and returns a string.

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

Try it online!

Commented

s =>             // s[] = input array
Buffer(          // turn into a Buffer:
  s.flatMap(c => //   for each character c in s[]:
    a > (        //     compare the current value of the accumulator
      a -=       //     with the updated value obtained by subtracting
        c + 1    //     either +1 or -1, according to c
    ) ?          //     if the accumulator was decremented (c = '+'):
      []         //       force flatMap() to ignore this entry
    :            //     else (c = '-'):
      1 - a      //       return -(accumulator - 1)
                 //       (with implicit modulo 256)
    a = 0        //     start with accumulator = 0
  )              //   end of flatMap()
) + ""           // end of Buffer(); coerce it to a string
fixed for an edge case
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (Node.js), 49 bytes

Expects an array of characters and returns a string.

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

Try it online!Try it online!

Commented

s =>             // s[] = input array
Buffer(          // turn into a Buffer:
  s.flatMap(c => //   for each character c in s[]:
    ca <> '-'( ?       //     ifcompare cthe iscurrent value of the accumulator
      a '+':-=       //     with the updated value obtained by subtracting
      ++a && c + 1    //     either +1 incrementor -1, according to c
    ) ?          //     if the accumulator was decremented (c = '+'):
      []         //       and force flatMap() to ignore this entry
    :            //     else (c = '-'):
      a-1 -, a      //       return the -(accumulator and decrement- it1)
                 //       a(with implicit modulo 256 is implicitly applied)
    a = 0        //     start with accumulator = 0
  )              //   end of flatMap()
) + ""           // end of Buffer(); coerce it to a string

JavaScript (Node.js), 49 bytes

Expects an array of characters and returns a string.

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

Try it online!

Commented

s =>             // s[] = input array
Buffer(          // turn into a Buffer:
  s.flatMap(c => //   for each character c in s[]:
    c < '-' ?    //     if c is a '+':
      ++a &&     //       increment the accumulator
      []         //       and force flatMap() to ignore this entry
    :            //     else:
      a--,       //       return the accumulator and decrement it
                 //       a modulo 256 is implicitly applied
    a = 0        //     start with accumulator = 0
  )              //   end of flatMap()
) + ""           // end of Buffer(); coerce it to a string

JavaScript (Node.js), 49 bytes

Expects an array of characters and returns a string.

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

Try it online!

Commented

s =>             // s[] = input array
Buffer(          // turn into a Buffer:
  s.flatMap(c => //   for each character c in s[]:
    a > (        //     compare the current value of the accumulator
      a -=       //     with the updated value obtained by subtracting
        c + 1    //     either +1 or -1, according to c
    ) ?          //     if the accumulator was decremented (c = '+'):
      []         //       force flatMap() to ignore this entry
    :            //     else (c = '-'):
      1 - a      //       return -(accumulator - 1)
                 //       (with implicit modulo 256)
    a = 0        //     start with accumulator = 0
  )              //   end of flatMap()
) + ""           // end of Buffer(); coerce it to a string
added a commented version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
saved 3 bytes
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading