| Bytes | Lang | Time | Link |
|---|---|---|---|
| 097 | BrainChild | 241101T051504Z | ATaco |
| 059 | C gcc | 241031T181525Z | ayaan098 |
| 020 | Japt hP | 241031T123941Z | Shaggy |
| 037 | J | 241031T055126Z | south |
| 076 | Kotlin | 240427T180653Z | Cactusro |
| 014 | Vyxal 3 | 240425T125812Z | pacman25 |
| 041 | PowerShell Core | 240522T203610Z | Julian |
| 057 | brainfuck | 240522T121642Z | matteo_c |
| 014 | 05AB1E | 240426T094646Z | Kevin Cr |
| nan | JavaScript | 240425T124028Z | None1 |
| 100 | C# | 240502T200529Z | Ezlandin |
| 059 | YASEPL | 240426T140959Z | madeforl |
| 015 | Stax | 240502T211426Z | recursiv |
| 035 | Perl 5 p | 240425T131652Z | Nahuel F |
| 066 | Python 3 | 240427T154529Z | Jonathan |
| 014 | Jelly | 240427T145710Z | Jonathan |
| 158 | Pascal | 240426T133707Z | Kai Burg |
| 024 | Perl 5 + p055 | 240425T132330Z | Dom Hast |
| 046 | C gcc | 240425T153703Z | Blue |
| 057 | JavaScript Node.js | 240425T124042Z | Arnauld |
| 100 | Python | 240427T015551Z | None1 |
| 123 | Scala 3 | 240425T235545Z | 138 Aspe |
| 019 | Charcoal | 240425T130308Z | Neil |
| 026 | Uiua 0.11.0 | 240425T201207Z | Tbw |
| 149 | Lua | 240426T190237Z | VBW |
| 037 | AWK vRS=. | 240425T200120Z | corvus_1 |
| 025 | Pip | 240425T192601Z | math sca |
| 093 | Google Sheets | 240426T013251Z | z.. |
| 047 | Funge98 | 240425T164150Z | Alt Shif |
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)
}
}
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.
Japt -hP, 20 bytes
q-
ÆËo+ ÅÊÃåÄ ËuG² d
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
J, 37 bytes
[:(]#a.{~256|[:+/\[-_1|.!.0])/'+-'=/]
Shameless port of Tbw's amazing Uiua answer.
[:(]#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
Kotlin, 76 bytes
{fold(0){a,c->when(c){'-'->{print(a.toChar());a-1}'+'->a+1 else->a}and 255}}
-1 byte thanks to ceilingcat.
Vyxal 3, 14 bytes
'-sᵛ'+Cv@₇%ꜝOṪ
would be 14 if vyxal didnt automatically treat + as a regex symbol
Edit-- Bug was fixed
PowerShell Core, 41 bytes
switch($args){+{$c++}-{[char]($c--%256)}}
Function that takes a string using splatting
Returns an array of characters
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)
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('++++++++++++++++++++++++++++++++++--'))
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));}
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+
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
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.
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)
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).
Perl 5 + -p055, 24 bytes
-2 bytes thanks to @Neil!
$-+=y/+//;$_=chr$---%256
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!).
C (gcc), 49 47 46 bytes
c;f(char*s){for(;*s;)*s++%5?c++:putchar(c--);}
Pretty straightforward.
-1 byte thanks to Arnauld.
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))+""
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.
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))
}
}
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
Uiua 0.11.0, 26 bytes SBCS
▽:+@\0◿256\+-⬚0↻¯1,∩=@+,@-
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
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
AWK -vRS=(.), 37 bytes
RT~/+/{x++}RT~/-/{printf"%c",x--%256}
Uses a regex as a record seperator to split the input into seperate characters and matches them one-by-one.
Pip, 29 25
C{i+:Dai+1%:256}M#*H:a^'-
- 4 bytes thanks to DLosc
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.
Google Sheets, 93 bytes
=SORT(JOIN(,IFNA(CHAR(SCAN(,SPLIT(A1,"-",,),LAMBDA(a,c,MOD(a+LEN(c),256)))-COLUMN(A:ZZ)+1))))
Funge-98, 47 bytes
>~#<:'+w$:'U3*-3jc+1_$0
^#:w-' <j7-1_$'U3*4j6:,
The program never halts, so if you run it using TIO, you have to manually cancel execution in order to view the output.