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.

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

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

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

CJam, 50 Bytes

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

Try it online!

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

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

\$\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)
+-+----+-+
| |    | |
+-+----+-+
| |    | |
| |    | |
+-+----+-+
| |    | |
+-+----+-+
\$\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]}
\$\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
\$\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.