3
\$\begingroup\$

This question already has an answer here:

Inspired by Braille graphics

Given a Boolean matrix (i.e., consisting of only 1s and 0s), output an ASCII-art-style Unicode representation of the closed-polygon shape created by the 1s in the matrix, using the Unicode drawing characters ┌ ─ ┐ │ └ ┘ (code points 0x250c, 0x2500, 0x2510, 0x2502, 0x2514, and 0x2518, respectively, separated by spaces). Replace the 0s with spaces, so you're left with just the line drawing. Output or return that drawing.

You won't have to handle invalid or ambiguous input - there will be only one unambiguous path of 1s through the matrix. In other words, every 1 has exactly two cardinal neighbors that are 1s, and two that are 0s or out of bounds. Additionally, since the shape is a closed-polygon, this guarantees that the path is cyclical.

Examples:

[[1,1,1]
 [1,0,1]
 [1,1,1]]

┌─┐
│ │
└─┘

[[0,1,1,1,1,0],
 [1,1,0,0,1,1],
 [1,0,0,0,0,1],
 [1,1,1,1,1,1]]

 ┌──┐
┌┘  └┐
│    │
└────┘

[[1,1,1,0,0]
 [1,0,1,1,1]
 [1,0,0,0,1]
 [1,1,0,1,1]
 [0,1,1,1,0]]

┌─┐
│ └─┐
│   │
└┐ ┌┘
 └─┘

[[1,1,1,0]
 [1,0,1,1]
 [1,1,0,1]
 [0,1,1,1]]

┌─┐
│ └┐
└┐ │
 └─┘
  • Leading or trailing newlines or whitespace are all optional, so long as the characters themselves line up correctly.
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
\$\endgroup\$

marked as duplicate by AdmBorkBork code-golf Aug 31 '17 at 19:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ What are the codepoints for the relevant drawing characters? \$\endgroup\$ – Peter Taylor Aug 31 '17 at 18:24
  • \$\begingroup\$ @PeterTaylor Good point. Added. \$\endgroup\$ – AdmBorkBork Aug 31 '17 at 18:29
  • 1
    \$\begingroup\$ I believe this is a dupe since my Jelly solution works with this input TIO. It could probably be shortened since there will only be one path here instead of multiple like in the other. \$\endgroup\$ – miles Aug 31 '17 at 18:44
  • \$\begingroup\$ @miles Dagnabbit. I thought this was a duplicate, but 48+ hours in the Sandbox and several searches by me didn't find one. \$\endgroup\$ – AdmBorkBork Aug 31 '17 at 19:14
  • \$\begingroup\$ @AdmBorkBork The usual tip: after you type the title, a list with possible duplicates goes below the title. You can check there, although the titles in this case were quite different. \$\endgroup\$ – Erik the Outgolfer Sep 1 '17 at 8:08
0
\$\begingroup\$

Javascript (ES6), 125 bytes

a=>a.map((r,i)=>r.map((c,j)=>c&&"─ ┌└  ┐┘ |"[!(a[i-1]||0)[j]+!(a[i+1]||0)[j]*2+!r[j-1]*4+!r[j+1]*8-3]||" ").join``).join`
`

Example code snippet:

f=
a=>a.map((r,i)=>r.map((c,j)=>c&&"─ ┌└  ┐┘ |"[!(a[i-1]||0)[j]+!(a[i+1]||0)[j]*2+!r[j-1]*4+!r[j+1]*8-3]||" ").join``).join`
`
o.innerText=f([[1,1,1,0,0],
[1,0,1,1,1],
[1,0,0,0,1],
[1,1,0,1,1],
[0,1,1,1,0]])
<pre id=o>

\$\endgroup\$

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