| Bytes | Lang | Time | Link |
|---|---|---|---|
| 146 | JavaScript ES7 | 250312T084104Z | Arnauld |
| 098 | Charcoal | 250312T101819Z | Neil |
| 044 | 05AB1E | 250312T090414Z | Kevin Cr |
JavaScript (ES7), 146 bytes
s=>s.replace(/(AA )?A+(,)?/g,(s,P,S)=>x=["32_10+-&*^"[s.length+4*!P-4*!S],...x],x=[])&x.map(o=>x.push(v=1/o?o:(eval(x.pop()+o+x.pop())**2)**.5))|v
Method
We first split the input string into tokens, using the following regular expression:
/(AA )?A+(,)?/g
\___/ \/\_/
| | '--> S = optional suffix \
| '----> main part |--> s = full match
'---------> P = optional prefix /
We turn each token into an integer in the range \$[0\dots9]\$ by applying the formula:
s.length + 4 * !P - 4 * !S
We translate this integer back into an output token, which is either a digit or a JS operator.
This gives:
| Input token | N=length | !P | !S | N+4*!P-4*!S | Name | Output token |
|---|---|---|---|---|---|---|
AAAA |
4 | 1 | 1 | 4 | Zero | 0 |
AAA |
3 | 1 | 1 | 3 | One | 1 |
A |
1 | 1 | 1 | 1 | Two | 2 |
AA A |
4 | 0 | 1 | 0 | Three | 3 |
AA A, |
5 | 0 | 0 | 5 | Sum | + |
AA AA, |
6 | 0 | 0 | 6 | Diff | - |
AAA, |
4 | 1 | 0 | 8 | Prod | * |
AA AAA, |
7 | 0 | 0 | 7 | And | & |
AAAA, |
5 | 1 | 0 | 9 | Xor | ^ |
We store these tokens in reverse order in an array x[] and iterate on this array to evaluate the expression:
x.map(o => // for each value o in x[]:
x.push( // push in x[] (reused as a stack):
v = // store in v
1 / o ? // if o is a digit:
o // use o
: // else (o is an operator):
( //
eval( // evaluate:
x.pop() + // the 1st operand
o + // followed by the operator
x.pop() // followed by the 2nd operand
) ** 2 // compute the absolute value
) ** .5 // (this is shorter than Math.abs())
) // end of push()
) // end of map()
| v // return the last result
Charcoal, 98 bytes
F⮌⪪⪫⪪⪫⪪S×A³,,¦AA ¦,¦ ≡ι,A,⊞υ⁺⊟υ⊟υ,AA,⊞υ↔⁻⊟υ⊟υ,,,⊞υ×⊟υ⊟υ,,,,⊞υ&⊟υ⊟υ,,A,⊞υΣ⁻…⮌υ²&⊟υ⊟υ⊞υ﹪⌕⪫,A⪫,,ιA⁴Iυ
Try it online! Link is to verbose version of code. Explanation:
F⮌⪪⪫⪪⪫⪪S×A³,,¦AA ¦,¦ ≡ι
Take the input string, replace AAA with ,, and AA with ,, then split on spaces and switch over the tokens in reverse order.
,A,⊞υ⁺⊟υ⊟υ
AA A, becomes ,A,; Plus the top two elements of the stack.
,AA,⊞υ↔⁻⊟υ⊟υ
AA AA, becomes ,AA,; Absolute Minus the top two elements of the stack.
,,,⊞υ×⊟υ⊟υ
AAA, becomes ,,,; Times the top two elements of the stack.
,,,,⊞υ&⊟υ⊟υ
AA AAA, becomes ,,,,; BitwiseAnd the top two elements of the stack.
,,A,⊞υΣ⁻…⮌υ²&⊟υ⊟υ
AAAA, becomes ,,A,, which has to be emulated as Charcoal does not have the XOR operator.
⊞υ﹪⌕⪫,A⪫,,ιA⁴
Otherwise, work out the value of the constant and push that to the stack.
Iυ
Output the final value on the stack.
05AB1E, 54 52 44 bytes
ðì•9X':;üÞVb•…,A ÅвJ•ZΩнG•S£"^&*α+0132"S:R.V
Try it online or verify all test cases.
Explanation:
ðì # Prepend a space to the (implicit) input-string
•9X':;üÞVb• '# Push compressed integer 163244455251778732342
…,A Åв # Convert it to custom-base ",A ",
# aka, convert to base-3 and index into ",A "
J # Join this character-list together to a string:
# "AAAA, AA AAA, AAA, AA AA, AA A,AAAAAAAAA AA"
•ZΩнG• # Push compressed integer 585764341
S # Convert it to a list of digits
£ # Split the earlier string into parts of these sizes:
# ["AAAA,"," AA AAA,"," AAA,"," AA AA,"," AA A,","AAAA","AAA","AA A","A"]
"^&*α+0132" # Push string "^&*α+0132"
S # Convert it to a list of characters
: # Replace the earlier substrings to these characters
R # Reverse the entire string
.V # Evaluate it as 05AB1E code
# (after which the result is output implicitly)
See this 05AB1E tip of mine (section How to compress large integers?) to understand why •9X':;üÞVb• is 163244455251778732342 and •ZΩнG• is 585764341.
05AB1E executes its stack in Reverse Polish notation, hence the need for the R and why .V works on the (transliterated) string as a whole without any other modifications.