15
\$\begingroup\$

I am shamelessly posting a request for something I would actually find useful. The task is to take an arbitrary ascii box drawing like this

     |
+----+----+
| state A +---+
+---------+   |
              |
         +----v----+
         | state B |
         +---------+

... and turn it into something more beautiful using unicode box drawing characters, e.g.

     │
╭────┴────╮
│ state A ├───╮
╰─────────╯   │
              │
         ╭────v────╮
         │ state B │
         ╰─────────╯

In more detail:

  • convert only + - | characters - other characters should be unchanged
  • use

    • BOX DRAWINGS LIGHT VERTICAL (U+2502) │
    • BOX DRAWINGS LIGHT HORIZONTAL (U+2500) ─
    • BOX DRAWINGS LIGHT ARC DOWN AND RIGHT (U+256D) ╭
    • BOX DRAWINGS LIGHT ARC DOWN AND LEFT (U+256E) ╮
    • BOX DRAWINGS LIGHT ARC UP AND LEFT (U+256F) ╯
    • BOX DRAWINGS LIGHT ARC UP AND RIGHT (U+2570) ╰
    • BOX DRAWINGS LIGHT VERTICAL AND LEFT (U+2524) ┤
    • BOX DRAWINGS LIGHT VERTICAL AND RIGHT (U+251C) ├
    • BOX DRAWINGS LIGHT DOWN AND HORIZONTAL (U+252C) ┬
    • BOX DRAWINGS LIGHT UP AND HORIZONTAL (U+2534) ┴
    • BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL (U+253C) ┼

    • - always replaced with U+2500

    • | always replaced with U+2502
    • + is replaced with a unicode character that depends on the 4 characters north, south, east and west of it (if they exist)
    • < and > are treated as vertical wall segments if north or south of a + (so that you can have arrows terminating in vertical walls of a box)
    • v and ^ are treated as horizontal wall segments if east or west of a + (so that you can have arrows terminating in horizontal walls of a box)
    • + is treated as a wall segment if either north, south, east or west of a + (so that lines can connect next to a box corner)

Testcases

+-+     +---+   +---+   |
| +--   |ABC|  -+   |  +++
+-+     ++--+   +---+  +-+
         |
         +--->
  |           +--+
+-v+   +---+  |  |  +-----+
|Hi|  ->   |  +^-+  |world<-----+
+--+   +---+   |    +-----+     |
               |                +--
   |
---+---
   |

Becomes

╭─╮     ╭───╮   ╭───╮   │
│ ├──   │ABC│  ─┤   │  ╭┴╮
╰─╯     ╰┬──╯   ╰───╯  ╰─╯
         │
         ╰───>
  │           ╭──╮
╭─v╮   ╭───╮  │  │  ╭─────╮
│Hi│  ─>   │  ╰^─╯  │world<─────╮
╰──╯   ╰───╯   │    ╰─────╯     │
               │                ╰──
   │
───┼───
   │

Shortest code wins!

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Where is the "intersection", i.e. if the + has lines around it everywhere? \$\endgroup\$
    – Leaky Nun
    May 25, 2016 at 10:48
  • 1
    \$\begingroup\$ You probably mean Unicode rather than UTF-8 \$\endgroup\$
    – Luis Mendo
    May 25, 2016 at 11:01
  • 1
    \$\begingroup\$ Could we have some test cases? Particularly for edge cases like where two boxes touch at an edge or corner (or clarification that this will never happen). \$\endgroup\$ May 25, 2016 at 11:10
  • 2
    \$\begingroup\$ Would be nice to post the required outputs for those test cases. \$\endgroup\$
    – manatwork
    May 25, 2016 at 11:41
  • 2
    \$\begingroup\$ Ugh, I was just composing an answer :-( \$\endgroup\$
    – Neil
    May 25, 2016 at 20:44

1 Answer 1

2
\$\begingroup\$

JavaScript (ES6), 236 bytes

s=>`
${s}
`.split`
`.map((l,i,a)=>l.replace(/[+-|]/g,(c,j)=>c>`-`?`│`:c>`+`?`─`:`┼┬├╭┤╮??┴?╰?╯`[g(a[i-1][j])+g(l[j-1],1)*2+g(l[j+1],1)*4+g(a[i+1][j])*8]),g=(c,f)=>(f?`+-^v`:`+<>|`).indexOf(c)<0).slice(1,-1).join`
`
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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