15
\$\begingroup\$

A room can be made up of connected rectangles, for instance an L-shaped room. Such a room can be described by a list of dimensions describing the size of each rectangle.

Assume you have two input lists. The first contains the width of rectangles stacked vertically over each other. The second contains the height of the rectangles.

As an example, the input [4 6][3 2] will be a 4-by-3 rectangle on top of a 6-by-2 rectangle. The figure below shows this shape. Note that the walls are considered "thin", thus it's the spaces between the wall that's determined by the input.

[4 6][3 2]
 ____
| |
| |
| |_
| |
|______|

The challenge is: Take a list of dimensions as input, and output the shape of the room as ASCII-art. The format must be as in the sample figures:

  • All horizontal walls are shown using underscores
  • All vertical walls are shown using bars
  • There shall be no walls where the rectangles are connected
  • The left wall is straight
  • For more details, have a look at the test cases

Assumptions you can make:

  • All dimensions are in the range [1 ... 20]
    • All horizonal dimensions are even numbers
  • The number of rectangles will be in the range [1 ... 10]
  • Only valid input is given
  • Optional input format (you can decide the order of the input dimensions, please specify in the answer).

Test cases:

[2][1]
 __
|__|
---
[4][2]
 ____
| |
|____|
---
[2 6 2 4][2 2 1 3]
 __
| |
| |___
| |
| ___|
| |_
| |
| |
|____|
---
[2 14 6 8 4 18 2 10 4 2][1 2 3 1 2 1 1 1 2 1]
 __
| |___________
| |
| _______|
| |
| |
| |_
| ___|
| |
| |_____________
| _______________|
| |______
| ____|
| |
| _|
|__|
asked Mar 16, 2016 at 13:16
\$\endgroup\$
3
  • \$\begingroup\$ You can decide the order of the input dimensions, does that mean we can swap rows and columns, and reverse them? Like this: your example input format: [2 14 6 8 4 18 2 10 4 2][1 2 3 1 2 1 1 1 2 1] -> (swap and reverse) -> my input format: [1 2 1 1 1 2 1 3 2 1][2 4 10 2 18 4 8 6 14 2] \$\endgroup\$ Commented Mar 19, 2016 at 22:28
  • \$\begingroup\$ Yes, that's ok.:-) \$\endgroup\$ Commented Mar 19, 2016 at 23:05
  • \$\begingroup\$ Thanks. Turned out I probably don't need to reverse them, just swap. \$\endgroup\$ Commented Mar 19, 2016 at 23:06

4 Answers 4

4
\$\begingroup\$

JavaScript (ES6) 174

The only critical part is the horizontal row joining 2 parts of different widths, with the vertical bar on right side that can be in the middle or at the right end.

(p,h,q=-1,R=(n,s=' ')=>s.repeat(n))=>[...p,0].map((x,i)=>(x>q?p=x:(p=q,q=x),(~q?`
|`+R(q+(x<p)-!x)+R(x>q,'|'):' ')+R(p+~q+!x,'_')+R(x<p,'|')+R(h[i]-1,`
|${R(q=x)}|`))).join``

TEST

f=(p,h,q=-1,R=(n,s=' ')=>s.repeat(n))=>[...p,0].map((x,i)=>(x>q?p=x:(p=q,q=x),(~q?`
|`+R(q+(x<p)-!x)+R(x>q,'|'):' ')+R(p+~q+!x,'_')+R(x<p,'|')+R(h[i]-1,`
|${R(q=x)}|`))).join``
// Less golfed
F=(p,h, q=-1, 
 R=(n,s=' ')=>s.repeat(n)
 )=>
 [...p, 0].map((x,i)=> (
 x>q? p=x : (p=q,q=x),
 (q>=0?`\n|`+R(q+(x<p)-!x)+R(x>q,'|'):' ')
 + R(p+~q+!x,'_') + R(x<p,'|')
 + R(h[i]-1,`\n|${R(q=x)}|`)
 )).join``
console.log=x=>O.textContent+=x+'\n'
;[ 
 [[2],[1]],
 [[4],[2]],
 [[2, 6, 2, 4],[2, 2, 1, 3]],
 [[2, 14, 6, 8, 4, 18, 2, 10, 4, 2],[1, 2, 3, 1, 2, 1, 1, 1, 2, 1]]
].forEach(t=>{
 var w=t[0],h=t[1]
 console.log('['+w+'] ['+h+']\n'+f(w,h)+'\n')
})
<pre id=O></pre>

answered Mar 16, 2016 at 20:07
\$\endgroup\$
4
\$\begingroup\$

Python 3, (削除) 230 (削除ここまで) (削除) 223 (削除ここまで) (削除) 222 (削除ここまで) 217 bytes

def f(a):b=a[0]+[0];m=' _';print('\n'.join([' '+'_'*b[0]+' ']+['|'+' '*min(b[l],b[l+1])+m[b[l+1]<1]*(b[l]>b[l+1])+m[k]*(b[l]-b[l+1]-1)+'|'+m[k]*(b[l+1]-b[l]-1)for l,i in enumerate(zip(*a))for k in[0]*(i[1]-1)+[1]]))

Thanks to @StewieGriffin @KevinLau for their help

Results

>>> f([[2, 14, 6, 8, 4, 18, 2, 10, 4, 2],[1, 2, 3, 1, 2, 1, 1, 1, 2, 1]])
 __ 
| |___________
| |
| _______|
| | 
| | 
| |_
| ___|
| | 
| |_____________
| _______________|
| |_______
| _____|
| |
| _|
|__|
answered Mar 16, 2016 at 14:45
\$\endgroup\$
3
  • \$\begingroup\$ Semicolons! They separate your assignments and save you from unwanted indentation! (a=1;b=2) \$\endgroup\$ Commented Mar 16, 2016 at 15:07
  • \$\begingroup\$ "solutoin" > solution \$\endgroup\$ Commented Mar 16, 2016 at 16:12
  • \$\begingroup\$ String indexing! m=' _' instead of m=[' ','_'] saves like 5 bytes. \$\endgroup\$ Commented Mar 17, 2016 at 19:05
3
\$\begingroup\$

Ruby, 139 bytes

Felt nostalgic and decided I wanted to golf down my first ever answer on this site with everything I've learned since then. I wasn't even using stabby lambdas or basic idioms like map in place of for loops for that answer!

->x,y{s=' ';puts s+?_*x[0]+s
y.zip(x,x[1..]){|i,n,m|puts"|#{s*n}|
"*~-i+?|+s*[n,m||=0].min+(m<1??_:m>n ??|:s)+?_*~-(m-n).abs+(m<n ??|:'')}}

Attempt This Online!

Ruby 191

First time golfing, also it's my first day with Ruby, so it's probably not the most elegant thing in the world, but it'll do?

def f(x)
a=x[0]+[0]
puts" #{'_'*a[0]} "
for i in 0..x[1].length-1
n,m=a[i,2].sort
puts"|#{' '*a[i]}|\n"*(x[1][i]-1)+'|'+' '*n+(a[i+1]<1?'_':m>a[i]?'|':' ')+'_'*(m-n-1)+(n<a[i]?'|':'')
end
end

Attempt This Online!

answered Mar 16, 2016 at 22:13
\$\endgroup\$
1
\$\begingroup\$

Retina, (削除) 169 (削除ここまで) (削除) 150 (削除ここまで) 113 bytes

Byte count assumes ISO 8859-1 encoding.

\d+
$*
{+r`1(1*¶[^|]*(1+))
1ドル¶|2ドル|
}` (¶.*) 1+
1ドル
1
_
(\|_+)\|(?=¶1円_(_+))
1ドル|2ドル
T`w` `(\|_+)_?(?=_*\|.*¶1円)
^¶
 

The code contains a trailing space on a trailing newline.

Input format:

Height (separated by spaces)
Width (also separated by spaces)

For example:

1 2 3 1 2 1 1 1 2 1
2 14 6 8 4 18 2 10 4 2

Try it online!

answered Mar 19, 2016 at 23:22
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.