19
\$\begingroup\$

Take two lists of non-negative integers, and output an ASCII-box as it's defined below.

  • Corners and intersections are pluses: + (ASCII-code 43)
  • Vertical lines are bars | (ASCII-code 124)
  • Horizontal lines are minuses - (ASCII-code 45)

The first input list specifies the number of minuses between each plus sign, in the horizontal direction.

The second input list specifies the number of bars between each plus sign, in the vertical direction.

It's easier to explain with a few examples:

0 // No minuses between each + sign
0 // No bars between + signs
++
++
------------------
1 2 // First a single minus, then two minuses 
1 2 // First one bar, then two bars
+-+--+
| | |
+-+--+
| | |
| | |
+-+--+
------------------
1 0 3 0 2 0
2 1 0 0
+-++---++--++
| || || ||
| || || ||
+-++---++--++
| || || ||
+-++---++--++
+-++---++--++
+-++---++--++

Clarifications:

  • Input order and format are optional
  • Only the boxes should be printed/displayed, but a trailing space or newlines are accepted.
  • You may optionally add 1 to all input values if that's more convenient. The second example would then be: 2 3; 2 3.

This is code golf, so the shortest code in bytes win.

asked Sep 18, 2016 at 10:55
\$\endgroup\$
1

9 Answers 9

6
\$\begingroup\$

Python 2, 117 Bytes

def f(h,v):r="+"+"+".join("-"*i for i in h)+"+\n";print r+r.join(("|"+"|".join(" "*i for i in h)+"|\n")*i for i in v)+r

Try it on ideone.

Not expecting too much from this. Really simple, just uses python joins and string multiplication to pull everything together.

answered Sep 18, 2016 at 11:40
\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6), 83 bytes

(a,b,g=(a,[s,t])=>t+a.map(n=>s.repeat(n)+t).join``+`
`)=>g(b,[g(a,` |`),g(a,`-+`)])

Output includes two trailing newlines.

answered Sep 18, 2016 at 13:44
\$\endgroup\$
4
  • \$\begingroup\$ Wow. So similar answers posted almost at the same time. ;) \$\endgroup\$ Commented Sep 18, 2016 at 13:50
  • \$\begingroup\$ (You beat me by 2 minutes and 2 bytes, though.) \$\endgroup\$ Commented Sep 18, 2016 at 13:53
  • \$\begingroup\$ @Arnauld You wasted time on your demo ;-) \$\endgroup\$ Commented Sep 18, 2016 at 14:01
  • \$\begingroup\$ Yeah, I guess so. ^^ Interestingly, my version would be 81 bytes with the two trailing newlines. \$\endgroup\$ Commented Sep 18, 2016 at 14:07
5
\$\begingroup\$

MATL, (削除) 25 (削除ここまで) (削除) 22 (削除ここまで) 21 bytes

'|-+ '2:"1tiYsQ(]E!+)

Uses inputs with 1 added (allowed by the challenge).

Try it online!

Explanation

The code initially builds an array containing 1 for the column indices of non-space characters in the final result, and 0 otherwise. So if the first input is [2 1 4 1 3 1] (would be [1 0 3 0 2 0] in the 0-based format) this array will be

1 0 1 1 0 0 0 1 1 0 0 1 1

Note how the length of runs of zeros is related to the input. Specifically, this array is built as follows:

  1. Initiallize the array to a single 1.
  2. Compute the cumulative sum of the input and add 1. In the example this gives [3 4 8 9 12 13].
  3. Extend the array from step 1 by assigning 1 to the entries with (1-based) indices given by step 2. Intermediate entries are automatically set to 0.

A similar array is built for the rows. Second input [3 2 1 1] (or [2 1 0 0 ]) gives

1 0 0 1 0 1 1 1

Now the second array is multiplied by 2, transposed and added with broadcast to the first. This gives the 2D array

3 2 3 3 2 2 2 3 3 2 2 3 3
1 0 1 1 0 0 0 1 1 0 0 1 1
1 0 1 1 0 0 0 1 1 0 0 1 1
3 2 3 3 2 2 2 3 3 2 2 3 3
1 0 1 1 0 0 0 1 1 0 0 1 1
3 2 3 3 2 2 2 3 3 2 2 3 3
3 2 3 3 2 2 2 3 3 2 2 3 3
3 2 3 3 2 2 2 3 3 2 2 3 3

Indexing into the string '|-+ ' gives the final result as a 2D char array. Since indexing is modular and 1-based, index 0 corresponds to the last element (space).

'|-+ ' % Push this string
 2:" ] % Do this twice
 1 % Push 1 (initial array)
 t % Push another 1 (contents to be filled in)
 i % Take input
 Ys % Cumulative sum
 Q % Add 1
 ( % Fill 1 into those entries of the array
 E % Multiply by 2
 ! % Transpose
 + % Add, with broadcast
 ) % Index (modular, 1-based) into the string
answered Sep 18, 2016 at 11:50
\$\endgroup\$
2
\$\begingroup\$

CJam, 50 Bytes

0lS/:i0++:H;lS/:i0+{H{'-*}%'+*N+o{H{S*}%'|*N+o}*}%

Try it online!

answered Sep 18, 2016 at 12:36
\$\endgroup\$
1
\$\begingroup\$

Pyth, 45 bytes

AQj.i*hlH]Js.i*hlG\+m*d\-G-mjb*d]XJ"+-""| "Hk

A program that takes input of two comma-separated lists on STDIN and prints the result.

There is probably still some golfing to be done here.

Try it online

Explanation coming later

answered Sep 18, 2016 at 23:06
\$\endgroup\$
1
\$\begingroup\$

Haskell, 55 bytes

f[a,b]x=a:do n<-x;(b<$[1..n])++[a]
g x=f[f"+-"x,f"| "x]

Defines a function g which takes the two input lists and returns a list containing the lines of the output

answered Sep 20, 2016 at 18:57
\$\endgroup\$
0
\$\begingroup\$

PowerShell v2+, 89 bytes

param($a,$b)($x="+$(($a|%{'-'*$_})-join'+')+")
$b|%{,"|$(($a|%{' '*$_})-join'|')|"*$_;$x}

Dang, don't think I can quite catch JavaScript.

Takes input $a and $b as explicit arrays. Sets variable $x to be the top row of the boxes based on looping through $a and some string concatenation. That's encapsulated in parens so it's placed on the pipeline. Then, we loop through $b, each iteration putting two strings on the pipeline -- the same style string but with spaces and | instead of hyphens and +, and $x. Those strings are all gathered from the pipeline with an implicit Write-Output at program completion, with default newline between them.

Examples

PS C:\Tools\Scripts\golfing> .\make-some-ascii-boxes.ps1 @(0) @(0)
++
++
PS C:\Tools\Scripts\golfing> .\make-some-ascii-boxes.ps1 @(1,0,3,0,2,0) @(2,1,0,0)
+-++---++--++
| || || ||
| || || ||
+-++---++--++
| || || ||
+-++---++--++
+-++---++--++
+-++---++--++
PS C:\Tools\Scripts\golfing> .\make-some-ascii-boxes.ps1 @(1,4,1) @(1,2,1)
+-+----+-+
| | | |
+-+----+-+
| | | |
| | | |
+-+----+-+
| | | |
+-+----+-+
answered Sep 19, 2016 at 14:48
\$\endgroup\$
0
\$\begingroup\$

Ruby, 66 bytes

->x,y{d=->c,m,z=x{c+z.map{|w|m*w}*c+c+$/}
d[d[?+,?-],d[?|,' '],y]}
answered Sep 19, 2016 at 17:28
\$\endgroup\$
0
\$\begingroup\$

Jelly, (削除) 30 (削除ここまで) 26 bytes

Ḣị"+-"| "ị@
1;+\Ṭ
Ç,ドル€/Ñ€Y

Test it at TryItOnline

How?

The input taken is a list of the two lists, [vertical, horizontal], and uses the incremented option
- so example 3 takes [[3,2,1,1], [2,1,4,1,3,1]]
Each of these is then converted to a boolean array indicating the rowType or rowCharacterType respectively, e.g. [[1,0,0,1,0,1,1,1], [1,0,1,1,0,0,0,1,1,0,0,1,1]]
The boxes are then constructed by making the rows from the characters identified by the rowType and rowCharacterType combinations - i.e. a rowType identifies either "+-" or "| " and a rowCharacterType identifies one of those two characters.

Ḣị"+-"| "ị@ - Link 1, create a row: [rowType, [rowCharaterTypes]]
Ḣ - head - get the rowType
 "+-"| " - list of strings ["+-", "| "]
 ị - index into (if rowType is 1 "+-"; if rowType is 0 "| ")
 ị@ - index into with reversed operands (index into that from rowCharaterTypes)
 (replace each 1 in rowCharaters with "+" or "|" and each 0 with "-" or " ")
1;+\Ṭ - Link 2, create the Type lists from the inputs: int[] nCharacters
1; - 1 concatenated with the input
 +\ - reduce with addition (accumulation provides the indices)
 Ṭ - boolean array with 1s at those indices
 As an example take the vertical of example 3:
 [3,2,1,1] -> [1,3,2,1,1] -> [1,4,6,7,8] -> [1,0,0,1,0,1,1,1]
 each of which will be passed as a rowType for Link 1
Ç,ドル€/Ñ€Y - Main link, takes one argument: [Vertical, Horizontal] (incremented option)
Ç€ - call the last link (2) for each of the two lists in the input
 ,€/ - pair each and reduce (making a list of [rowtype [rowCharacterTypes]])
 р - call the next link (1) for each
 Y - join on line feeds
answered Sep 21, 2016 at 0:49
\$\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.