Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

5 of 5
added 88 characters in body
Jonah
  • 34.1k
  • 4
  • 41
  • 95

J, 55 bytes

([:(1+_2-~/\])@I.,.@'-|'E.]);&(0-.~,)'\|+'#&>@rxall"1|:

Attempt This Online!

-12 after seeing rule change allowing lists of lengths as the output.

original answer outputting art, 78 67 bytes

([:('-'<@#~"+1+_2-~/\])@I.,.@'-|'E.]),&(a:-.~,)'\|+',.&.>@rxall"1|:

Try it online!

Note: Regex in J is broken on TIO, so the link above is just for the byte count. I will paste below some output run on my computer on J903.

Also, note that we pre-process the input into lines of text, and that J requires boxing for heterogeneous lists, hence the boxed output.

echo f ];._2 ] 0 :0
--- ----
| |--- | |
---| |
| || |
)
echo f ];._2 ] 0 :0
---
| |
--------
||| |
)

Outputs:

┌───┬────┬───┬───┬─┬─┬─┬─┬─┬─┬─┬─┐
│---│----│---│---│|│|│|│|│|│|│|│|│
│ │ │ │ │ │ │ │ │|│|│ │ │
└───┴────┴───┴───┴─┴─┴─┴─┴─┴─┴─┴─┘
┌───┬──┬──────┬─┬─┬─┬─┬─┬─┐
│---│--│------│|│|│|│|│|│|│
└───┴──┴──────┴─┴─┴─┴─┴─┴─┘

how

  • The "legs" part is just doing a regex search for repeated pipes \|+ in the transpose of the input, and then converting it back to one pipe per line.

  • The "tabletop" part is slightly more interesting, because we have to account for tables that are pushed up next to each other -- ie, sometimes we can only find the demarcations between tabletops by looking at the legs. J has a built-in ability to search for box-shape strings withing a 2D grid, and so we use E. to find instances of:

    -
    |
    

    within the input. We then do some arithmetic on those found indexes to find the length of all tabletops, and convert those lengths back to ascii strings like -----.

Jonah
  • 34.1k
  • 4
  • 41
  • 95

AltStyle によって変換されたページ (->オリジナル) /