11
\$\begingroup\$

Marshall Lochbaum's online BQN REPL has an interesting(and aesthetically pleasing) method of displaying arrays. Your task is to implement a version with simpler rules.

[2,[3],4,[[[6,[5],7]]]]

becomes:

┌─                                 
· 2 ┌·    4 ┌·                     
    · 3     · ┌·                   
        ┘     · ┌─                 
                · 6 ┌·    7        
                    · 5            
                        ┘          
                            ┘      
                              ┘    
                                ┘  
                                  ┘

Rules for drawing

  • A singleton array has
┌· 
·

as its top left corner.

  • A non-singleton array has
┌─  
·

as its top left corner.

(In the actual BQN, this is used for rank-0, or "unit" arrays)

  • All arrays have

as their bottom right corner.

  • Each element at a certain depth will be displayed at the same depth vertically. For example, 2 and 4 are displayed 1 unit from the top since they are at depth 1.
  • Each further level of nesting depth moves 1 unit down in depth.
  • Numbers next to each other will be separated by a single space.

Other details

  • You may take a ragged numeric array in any way your language supports.
  • The input will only consist of integers and nested arrays containing integers.
  • If your language does not support arrays with depth, you may take a string/other equivalent that your language supports.
  • If your language's codepage does not contain the characters ┌ · ─ ┘, you can count them as 1 byte.
  • This is . Shortest answer in each language wins.
  • Since bacon strips are thin, your code must be thin and small as well.
\$\endgroup\$
5
  • \$\begingroup\$ Nice challenge, though I feel like "Take a nested array, and 'pull down' elements one line for each depth of nest", with none of the special character requirements, might be the purer form of it. \$\endgroup\$ – Jonah Apr 24 at 16:27
  • 1
    \$\begingroup\$ @Jonah That is a good idea. I'll sandbox it. \$\endgroup\$ – Razetime Apr 24 at 16:28
  • \$\begingroup\$ I realized you'd still need a way to show arrays ending -- eg, if you had two adjacent nested arrays. That could be printed on the same line though. \$\endgroup\$ – Jonah Apr 25 at 0:36
  • \$\begingroup\$ @Jonah seems like this is the exact same idea, except upside down. \$\endgroup\$ – Razetime Apr 25 at 4:42
  • \$\begingroup\$ Yeah it’s too close \$\endgroup\$ – Jonah Apr 25 at 5:03
5
\$\begingroup\$

Charcoal, 72 bytes

FS≡ι(«⊞υⅉP⌈┌´·↘→⊞υⅉ»)«≔⊕⊟υιM¹⁻ιⅉ┘M⁻ⅉ⊟υ↑¿υ⊞υ⌈⟦ι⊟υ⟧»,«↑≔KD⊕ⅈ←ι§≔ι⊖⌕ι┌¦─↘»ι

Try it online! Link is to verbose version of code. Uses lists rather than arrays because they're easier to input. Explanation:

FS≡ι

Switch over each character of the input as a string.

For a (, ...

⊞υⅉ

... save the vertical position as the top margin, ...

P⌈┌´·↘→

... assume a single-element list, ...

⊞υⅉ

... and save the vertical position again, this time as the bottom margin.

»)«

For a ), ...

≔⊕⊟υι

... get the bottom margin of this list, plus 1, ...

M¹⁻ιⅉ

... move to that row, ...

... close the list, ...

M⁻ⅉ⊟υ↑

... move to and forget the top margin of this list, ...

¿υ⊞υ⌈⟦ι⊟υ⟧

... and update the bottom margin of the containing list if necessary.

»,«

For a ,, ...

↑≔KD⊕ⅈ←ι

... grab the text on the previous row, ...

§≔ι⊖⌕ι┌¦─

... find the and change the character after it to a , ...

... and move to the next column on the original row.

»ι

Otherwise, just output the current character.

Note that the · is in Charcoal's code page (so because it needs to be quoted it ends up costing me two bytes!) but the box-drawing characters are not. They would normally cost three bytes in Charcoal so as I use them four times I have subtracted 8 bytes from Charcoal's calculation.

\$\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.