17
\$\begingroup\$

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

  1. Numbers will always be single-digit
  2. Since operators are one symbol shorter than numbers, they are placed one symbol lower than numbers, leaving whitespace on top.
  3. All symbols have a column of whitespace between them
  4. This is a code-golf challenge, so lowest byte count wins!
\$\endgroup\$
  • \$\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 May 10 at 14:58
  • 1
    \$\begingroup\$ @JonathanAllan yes \$\endgroup\$ – Dion May 10 at 15:10
  • 4
    \$\begingroup\$ Would have been funnier to ouput the result :) \$\endgroup\$ – Olivier Grégoire May 11 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 May 11 at 12:29
  • 1
    \$\begingroup\$ @Dion But printing calculated result as ASCII would make evaluation less useless I think? \$\endgroup\$ – val says Reinstate Monica May 11 at 13:29
14
\$\begingroup\$

APL (Dyalog Unicode), 46 44 bytes

'/(80-)63+92145*7'[16|43|25⊥¨(×⊆⊢)2⊥' .\'⍳⎕]

Try it online!

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

| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

Python 2, 154 139 bytes

s=zip(*input().split('\n'))
i=0
while s[i:]:d=3**(max((s+[' '])[i+1])>' ')+i;print'+/5(_41_7802*6__-9)3'[hash(tuple(s[i:d]))*3%67%21];i=1+d

Try it online!

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

APL (Dyalog Unicode), 69 61 bytes

Anonymous tacit prefix function. Requires ⎕IO←0.

{('+-*/()',⎕D)['∆JV⍺TgucKmre3Ha'⍳⎕AV[94|2⊥¨,⌿⍵⊆⍨∨⌿32≠⍵]]}⎕UCS

Try it online!

⎕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)

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 67 bytes

⭆⪪⭆θ§αI⭆⟦θηζε⟧÷⊖℅§λκ³²A℅⁺⁴⁰÷⌕”$⌈O⁴εΦa↙M∕³↧FωG↙σ⎇C(|?⊞¦﹪ω←2I0C⁻sⅉ”ι³

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
                ℅                   Take the ASCII code
               ⊖                    Decrement
              ÷     ³²              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
                           ÷     ³  Integer divide by 3
                        ⁺⁴⁰         Plus literal `40`
                       ℅            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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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):''

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Jelly, 36 bytes

ỴZ⁶=ẠƊ€œp$“¢Ọ/’,“642-*+)9(8/17035”ḥⱮ

A monadic Link accepting a list of characters which yields a list of characters.

Try it online!

How?

ỴZ⁶=ẠƊ€œ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:
  ⁶                                  -     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
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.