| Bytes | Lang | Time | Link |
|---|---|---|---|
| 016 | Charcoal | 220328T214953Z | Neil |
| nan | J | 220328T231803Z | Jonah |
| 044 | Retina 0.8.2 | 220328T220146Z | Neil |
| 073 | C gcc | 220328T214011Z | user3604 |
| 010 | 05AB1E | 220328T213714Z | Kevin Cr |
Charcoal, 16 bytes
IE⌕Aθ=⁻ι⁺κ⊗№…θι-
Try it online! Link is to verbose version of code. Explanation:
θ Input string
⌕A Find all indices of
= Literal string `=`
E Map over indices
ι Current index
⁻ Subtract
κ Number of previous `=`
⁺ Plus
№…θι- Number of previous `-`
⊗ Doubled
I Cast to string
Implicitly print
The number any given = prints is equal to the number of +s so far minus the number of -s so far. The number of +s so far plus the number of -s so far plus the number of =s so far is of course the index of the = in question, so the number of +s plus -s can be calculated from the difference, and the difference between the number of +s and -s by further subtracting double the number of -s.
As @Jonah points out in his J answer, you can also translate the -=+ characters to the integers -1..1, and output the cumulative sums at the positions of the 0s. This also takes 16 bytes in Charcoal:
IE⌕Aθ=ΣE…θ⊕ι⌕=+λ
Try it online! Link is to verbose version of code. Explanation:
θ Input string
⌕A Find all indices of
= Literal string `=`
E Map over indices
θ Input string
… Truncated to length
ι Current index
⊕ Incremented
E Map over characters
λ Current character
⌕ Find index in
=+ Literal string `=+`
Σ Take the sum
I Cast to string
Implicitly print
Charcoal's Find returns -1 when there is no match, which is what we want for -.
J, 29 26 25 bytes
0(=echo"++/\@])'-='&i.-1:
-3 thanks to ovs!
-1 thanks to Neil!
Inspired by Neil's answer.
Consider +=+=:
'-='&i.-1:Convert-=+to_1 0 11 0 1 00(=...+/\@])Scan sum and filter only elements at the0positions:1 0 1 0 ... 1 1 2 2 1 2 NB. 1 and 2 are the values at the 0 positionsecho"+Print each value1 2
Retina 0.8.2, 44 bytes
=
=¶$`=
-1=A`
%O`.
+`\+-
((-)+|\+*)=+
$2$.1
Try it online! Link includes test cases. Explanation:
=
=¶$`=
Prepend all of the prefixes of the input that end in an =.
-1=A`
Delete the original input. (Assuming it ended in an =, this will have been prepended as the last prefix.)
%O`.
Sort the characters in each prefix.
+`\+-
Cancel out the +s with the -s.
((-)+|\+*)=+
$2$.1
Convert to decimal.
C (gcc), 73 bytes
f(char*p){int a=0;p--;while(*++p)*p==43?a++:*p==45?a--:printf("%d\n",a);}
Ungolfed:
void f(char*p){
int a=0; // Accumulator
p--; // The loop increases the pointer so compensate
while(*++p){
if(*p==43)a++; // 43 and 45 are ASCII codes for + and -
else if(*p==45)a--;
else printf("%d\n",a);
}
}
05AB1E, 10 bytes
΄+-„><‡.V
Try it online or verify all test cases.
Explanation:
Î # Push 0 and the input
„+- # Push string "+-"
„>< # Push string "><"
‡ # Transliterate all "+" to ">" and "-" to "<" in the input
.V # Evaluate and execute as 05AB1E code:
# `>`: Increase the top by 1
# `<`: Decrease the top by 1
# `=`: Print the top with trailing newline, without popping
Because = in 05AB1E does exactly what we want it to in +-=, it might be the perfect language for this challenge.