Boolean Rectangles

3

1

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.

AdmBorkBork

Posted 2017-08-31T18:12:49.307

Reputation: 41 581

Question was closed 2017-08-31T19:14:22.573

What are the codepoints for the relevant drawing characters? – Peter Taylor – 2017-08-31T18:24:26.403

@PeterTaylor Good point. Added. – AdmBorkBork – 2017-08-31T18:29:17.323

1

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.

– miles – 2017-08-31T18:44:09.447

@miles Dagnabbit. I thought this was a duplicate, but 48+ hours in the Sandbox and several searches by me didn't find one. – AdmBorkBork – 2017-08-31T19:14:02.580

@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. – Erik the Outgolfer – 2017-09-01T08:08:04.540

Answers

0

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>

Herman L

Posted 2017-08-31T18:12:49.307

Reputation: 3 611