The task
Given a multiline string with ascii art operators and numbers, parse the expression!
Possible symbols
Numbers: (3x4)
/
/|
|
---
___
_|
|
---
___
_|
|
---
| |
|_|
|
|
___
|_
|
---
___
|_
| |
---
___
/
/
|
___
|_|
| |
---
___
|_|
|
---
___
| |
| |
---
Operators: (3x3)
|
-+-
|
---
\ /
x
/ \
.
---
.
Braces: (1x4)
/
|
|
\
\
|
|
/
Input
A multiline string with ascii art operators and numbers. Examples in the test cases.
Output
The string of the parsed expression.
Test cases
Input:
/ ___ ___ \ ___
| _| | / | . _|
| | -+- / | --- |
\ --- | | / . ---
Output: (2+7)/3
Input:
___ / ___
|_| /| \ / |_
| --- | x |
--- --- / \ ---
Output: 9-1*5
Rules
- Numbers will always be single-digit
- Since operators are one symbol shorter than numbers, they are placed one symbol lower than numbers, leaving whitespace on top.
- All symbols have a column of whitespace between them
- This is a code-golf challenge, so lowest byte count wins!
-
\$\begingroup\$ May we assume the lines of text are all of the same length (filled with spaces on the right when necessary to do so)? \$\endgroup\$Jonathan Allan– Jonathan Allan2020年05月10日 14:58:59 +00:00Commented May 10, 2020 at 14:58
-
1\$\begingroup\$ @JonathanAllan yes \$\endgroup\$Dion– Dion2020年05月10日 15:10:16 +00:00Commented May 10, 2020 at 15:10
-
5\$\begingroup\$ Would have been funnier to ouput the result :) \$\endgroup\$Olivier Grégoire– Olivier Grégoire2020年05月11日 11:14:39 +00:00Commented May 11, 2020 at 11:14
-
\$\begingroup\$ @OliverGrégoire that was the og post, but it was quickly agreed that evaluating the expression was a useless part \$\endgroup\$Dion– Dion2020年05月11日 12:29:45 +00:00Commented May 11, 2020 at 12:29
-
1\$\begingroup\$ @Dion But printing calculated result as ASCII would make evaluation less useless I think? \$\endgroup\$val - disappointed in SE– val - disappointed in SE2020年05月11日 13:29:34 +00:00Commented May 11, 2020 at 13:29
7 Answers 7
APL (Dyalog Unicode), (削除) 46 (削除ここまで) 44 bytes
'/(80-)63+92145*7'[16|43|25⊥ ̈(×ばつ⊆⊢)2⊥' .\'⍳⎕]
⎕ evaluated input, must be a character matrix
' .\'⍳ replace spaces with 0, .s with 1, \s with 2, and everything else with 3
2⊥ base-2 decode the columns
(×ばつ⊆⊢) split on zeroes
25⊥ ̈ base-25 decode each group
16|43| mod 43, mod 16
'/(80-)63+92145*7'[ ] use as indices in the given string
APL (Dyalog Unicode), (削除) 69 (削除ここまで) 61 bytes
Anonymous tacit prefix function. Requires ⎕IO←0.
{('+-*/()',⎕D)['∆JV⍺TgucKmre3Ha'⍳⎕AV[94|2⊥ ̈,⌿⍵⊆⍨∨⌿32≠⍵]]}⎕UCS
⎕UCS convert characters to code points
{...} "dfn"; argument is ⍵
('+-*/()',⎕D) the involved symbols followed by the Digits
[...] index into that using the following indices:
'∆JV⍺TgucKmre3Ha'⍳ indices in this string (missing elements get index=length) of
⎕AV[...] index into the Atomic Vector (the character set) using the following:
94| the division remainder when divided by 94 of
2⊥ ̈ the base-2 evaluation of each of
,⌿ the vertically joined and flattened
⍵⊆⍨ segments of the argument, as indicated by trues in
∨⌿ the vertical OR-reduction of
32≠⍵ the Boolean mask indicating where the argument is different from 32 (space)
JavaScript (Node.js), 152 bytes
f=(s,i=0,a=s.split`
`,c=a[n=0][i],x=1)=>c?"*3-425()6+9/7081"[c=='/'?6:c=='\\'?7:[x=3,4,5,6,9].map(c=>n+=Buffer(a[c/3|0])[i+c%3]*c)|n%47%16]+f(s,i-~x):''
Charcoal, 67 bytes
⭆⪪⭆θ§αI⭆⟦θηζε⟧÷⊖c/o§λκ32Ac/o+40÷⌕"$⌈O4εΦa↙M∕3↧FωG↙σ⎇C(|?⊞¦%ω←2I0C−sj"ι3
Try it online! Link is to verbose version of code. Assumes 4 input strings of equal length (space padded if necessary). Explanation:
θ First input
⭆ Map over characters and join
⟦θηζε⟧ All four inputs as a list
⭆ Map over strings
λ Inner string
§ Indexed by
κ Outer index
c/o Take the ASCII code
⊖ Decrement
÷ 32 Integer divide by literal `32`
I Cast to integer
§α Index into uppercase alphabet
⪪ A Split on literal `A`
⭆ Map over substrings
⌕...ι Find their indices in compressed data
÷ 3 Integer divide by 3
+40 Plus literal `40`
c/o Convert to ASCII
Implicitly print
The compressed data represents the literal string GAARAATEYKBKAAAKKKAAAKHKRZRXFBDRNZRRYSFNRDRRDBIURRRNRR which contains the pattens that result from the calculation, padded with As to place them at the appropriate offset to generate the desired ASCII character.
Jelly, 36 bytes
ỴZ6=ẠƊ€œp$"¢Ọ/’,"642-*+)9(8/17035"ḥⱮ
A monadic Link accepting a list of characters which yields a list of characters.
How?
ỴZ6=ẠƊ€œp$"¢Ọ/’,"642-*+)9(8/17035"ḥⱮ - Link: list of characters, I
Ỵ - split at newline characters
Z - transpose
$ - last two links as a monad - i.e. f(X):
Ɗ€ - for each, last three links as a monad:
6 - the space character
= - equals? (vectorises)
Ạ - all?
œp - split (X) at truthy indices of (that)
- (...call the result A)
"¢Ọ/’ - base 250 int = 170548 (salt)
"642-*+)9(8/17035" - list of characters "642-*+)9(8/17035" (domain)
, - pair -> [salt, domain]
Ɱ - map across A with:
ḥ - Jelly's hash function with salt and domain
05AB1E, (削除) 54 (削除ここまで) (削除) 40 (削除ここまで) 39 bytes
"2(-1 0/ 9 6*)+"Ž ×ばつ¡JÇ2δβŽ4w%è
(削除) Can definitely be golfed some more with smarter magical integers (including changing the ⇠to for example Åβ or changing the order of the charcters in the strings), but I'm pretty bad at those kind of challenges personally, so this will do for now. (削除ここまで)
-14 bytes thanks to @ovs, because he is skilled in these kind of magic integer challenges. ^_^
Try it online or verify all test cases.
Explanation:
"2(-1 0/ 9 6*)+" # Push this string
Ž ́Š # Push compressed integer 45783
« # Append it to the string: "2(-1 0/ 9 6*)+45783"
| # Push all input-lines as list
€S # Convert each line to a list of characters
ø # Zip/transpose; swapping rows/columns
J # Join each inner list (column) together to a string
×ばつ # Push a string of 4 space characters: " "
¡ # Split the list on that
J # Join each inner list of strings to a single string
Ç # Convert each string to a list of codepoint integers
δ # For each list of integers:
2 β # Convert from a base-2 list to a base-10 integer
Ž4w% # Modulo-1078 on each value
è # And then (modulair 0-based) index them into the string
# (after which the result is output implicitly)
See this 05AB1E tips of mine (section How to compress large integers?) to understand why Ž ́Š is 45783 and Ž4w is 1078.
-
1
-
1\$\begingroup\$ @ovs Thanks! That's indeed a lot better. And in your approach I can remove the map for another -1, since most of the builtins vectorize. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年09月23日 08:47:44 +00:00Commented Sep 23, 2020 at 8:47