39
\$\begingroup\$

Given an array of positive integers in base 10, where n > 0, output their representation of a binary wall.

How does this work?

  1. Convert each number to its binary representation.
  2. Pad the representation with leading zeroes to the length of the longest one i.e. 1, 2 -> 1, 10 -> 01, 10.
  3. Create a wall where the 1s are bricks and 0s are missing bricks.

A wall is a block of characters where any printable character represents a brick and a space (ASCII decimal code 32) represents a missing brick. You may choose any character for the brick, it need not be distinct across the wall as long as it isn't a white space character. The missing brick character must be a space. For the example below I have used * for the bricks.

Example

Input:

[ 15, 7, 13, 11 ]
  1. [ 1111, 111, 1101, 1011 ]

  2. [ 1111, 0111, 1101, 1011 ]

  3. Output:

    ****
     ***
    ** *
    * **
    

Rules

  • Input must be taken in base 10, if your language accepts other bases you may not use them.
  • Leading and trailing new lines are allowed.
  • Input may be taken as a list of integers, separate arguments or any reasonable format.
  • Output may be in any reasonable format: new line separated string, array of lines, 2d array etc.
  • Standard loopholes are disallowed.

Test Cases

Note that in the first test case all of layers have an empty brick at the end.

[ 14, 4, 6, 2 ]

*** 
 *  
 ** 
  * 

[ 1, 2, 4, 8, 16 ]

    *
   * 
  *  
 *   
*

[ 15, 11, 15, 15 ]

****
* **
****
****

[ 11, 10, 9, 8 ]

* **
* * 
*  *
*   

This is code golf so shortest code wins!

\$\endgroup\$
10
  • \$\begingroup\$ Can the output be an array of lines or a 2d array of chars? \$\endgroup\$
    – ovs
    Commented Jul 24, 2017 at 11:23
  • \$\begingroup\$ @ovs Sorry thought I'd specified that, yes you can output an array or 2d array etc. Any reasonable format. \$\endgroup\$ Commented Jul 24, 2017 at 11:26
  • \$\begingroup\$ In the case of a 2D array, can we use numbers for the bricks instead of characters? e.g. [[1, " ", 1, " "], ...] \$\endgroup\$
    – Arnauld
    Commented Jul 24, 2017 at 13:27
  • \$\begingroup\$ @Arnauld Yeah that seems fine. \$\endgroup\$ Commented Jul 24, 2017 at 13:45
  • 1
    \$\begingroup\$ @Giuseppe New lines only, otherwise it will be confused for empty bricks. \$\endgroup\$ Commented Jul 24, 2017 at 15:46

37 Answers 37

14
\$\begingroup\$

MATL, 5 bytes

B42*c

Try it online!

Explanation

B     % Implicitly input an array of numbers. Convert to binary. 
      % Gives a matrix with each row corresponding to a number
42    % Push 42 (ASCII code of '*')
*     % Multiply
c     % Convert to char. Char 0 is displayed as space. Implicitly display
\$\endgroup\$
3
  • \$\begingroup\$ You may choose any character for the brick, it need not be distinct across the wall as long as it isn't a white space character. yeah that means you probably don't need 42* or something... \$\endgroup\$ Commented Jul 24, 2017 at 12:37
  • \$\begingroup\$ @EriktheOutgolfer I could choose any other number, but I do need those three bytes I think. \$\endgroup\$
    – Luis Mendo
    Commented Jul 24, 2017 at 12:47
  • \$\begingroup\$ What if there's a 1-byte builtin for 100 or some other number? \$\endgroup\$ Commented Jul 24, 2017 at 12:51
13
\$\begingroup\$

J, 8 bytes

' *'{~#:

Try it online!

Explanation

' *'{~#:  Input: array of integers
      #:  Convert each to binary with left-padding
' *'{~    Use the digits to index into the string ' *'
\$\endgroup\$
1
  • \$\begingroup\$ Oh, so #: is why this beats Jelly. \$\endgroup\$ Commented Jul 24, 2017 at 13:38
10
\$\begingroup\$

Jelly, 9 bytes

Bo⁶Uz⁶ZUY

Try it online!

EDIT: HOW J BEAT JELLY DAT IMPOSSIBLE >_<

\$\endgroup\$
9
\$\begingroup\$

Octave, 22 bytes

@(x)[dec2bin(x)-16,'']

Try it online

Explanation:

Saved some bytes thanks to Luis Mendo! Also, I didn't notice that I could choose which character to build the wall with, not only *.

@(x)                    % Take the input as a column vector
    dec2bin(x)          % Convert each row of the input to a string with 1 and 0
                        % It gets automatically padded with zeros, to fit the longest number
    dec2bin(x)-16       % Subtract 16, to get from the ASCII-values of 1/0 (48/49)
                        % to 32/33 (space and !)
@(x)[dec2bin(x)-16,'']  % Concatenate with the empty string to convert it to a string.

Or with de2bi:

Explanation:

@(x)                          % Take the input as a column vector
               de2bi(x)       % Convert each row of the input to a binary number.
                              % Gets automatically padded to fit the longest number
            42*de2bi(x)       % Multiply the matrix by 42, which is the ASCII-value for *
           [42*de2bi(x),'']   % Concatenate the matrix with the empty string to convert
                              % it to a string. 0 are automatically displayed as spaces
@(x)fliplr([42*de2bi(x),''])

The following works on TIO, for 7 bytes more:

@(x)fliplr([42*(dec2bin(x)>48),''])

Try it here

\$\endgroup\$
0
6
\$\begingroup\$

Python 3, 88 84 71 74 72 bytes

A lambda that returns a list of Strings, representing each line.

lambda n:[bin(x)[2:].replace(*'0 ').rjust(len(bin(max(n)))-2)for x in n]

Try it online! (link to the newline separated version)

Explanation

  • lambda n: - Creates an (anonymous) lambda, with a parameter n. Returns implicitly.

  • [...] - Creates a list comprehension.

  • bin(x)[2:] - Gets the binary representations of the numbers.

  • .replace(*'0 ') - Replaces all the occurrences of 0 with a space.

  • .rjust(len(bin(max(n)))-2) - Pads the binary representations to the length of the longest one.

  • for x in n - Iterates through n, with the variable x.


Changelog

  • -1 - 3 bytes thanks to @Rod, -(...)+2 = 2-(...), use of rjust()

  • Added a version with bin() instead, that was invalid since it didn't work for 1 and 2.

  • Fixed the bug above using format().

  • Changed return type to list of Strings, because it was allowed by the OP.

  • Fixed yet another bug using rjust() and switching back to bin(), spotted and fixed by @Rod.

\$\endgroup\$
0
6
\$\begingroup\$

JavaScript (ES6), 81 79 bytes

Saved 2 bytes by using numbers instead of characters for the bricks, as suggested by Rick Hitchcock

Returns a 2D array with 1's for the bricks.

f=(a,b=[],x=1)=>a.every(n=>n<x)?a.map(n=>b.map(i=>n&i?1:' ')):f(a,[x,...b],x*2)

Test cases

f=(a,b=[],x=1)=>a.every(n=>n<x)?a.map(n=>b.map(i=>n&i?1:' ')):f(a,[x,...b],x*2)

format = a => a.map(s => s.join``).join`\n`

console.log(format(f([ 14, 4, 6, 2 ])))
console.log(format(f([ 1, 2, 4, 8, 16 ])))
console.log(format(f([ 15, 11, 15, 15 ])))
console.log(format(f([ 11, 10, 9, 8 ])))

\$\endgroup\$
0
5
\$\begingroup\$

05AB1E, 9 bytes

bí.Bí»0ð‡

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Beat me by 38 seconds. I had b0ð:í.Bí» for 9 bytes as well. \$\endgroup\$
    – Riley
    Commented Jul 24, 2017 at 13:09
5
\$\begingroup\$

Haskell, 76 75 74 bytes

f x|all(<1)x=x>>[""]|a<-f$map(`div`2)x=zipWith(++)a$map(cycle[" ","*"]!!)x

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Ruby, 63 59 bytes

-4 bytes with help from Alexis Andersen

->*n{puts n.map{|i|("%#{('%b'%n.max).size}b"%i).tr'0',' '}}

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ you don't need the 0 in the string format. you could shave a byte by replacing n.max.to_s(2).size with ('%b'%n.max).size and you don't actually need to replace the 1 with * \$\endgroup\$ Commented Jul 24, 2017 at 17:51
  • \$\begingroup\$ @AlexisAndersen thanks :) \$\endgroup\$
    – daniero
    Commented Jul 24, 2017 at 19:48
5
\$\begingroup\$

R, 87 88 bytes

Wall blocks represented by an 8, because, well lots of eights.

write(ifelse((I=sapply(scan(),intToBits))[(M=max(which(I>0,T)[,1])):1,],8,' '),1,M,,'')

Try it online!

Input integer list is converted to array of bits which are trimmed of trailing 0 bits and reversed.

The reduced array is then output using write and a column width which was determined when the array was trimmed.

ifelse() is the only IF option that works on vectors unfortunately.

\$\endgroup\$
2
  • \$\begingroup\$ @Vlo pointed out you can use 1 rather than "" for the output file in write. \$\endgroup\$
    – Giuseppe
    Commented Mar 12, 2018 at 14:54
  • \$\begingroup\$ @Giuseppe thanks for the tip \$\endgroup\$
    – MickyT
    Commented Mar 12, 2018 at 17:54
4
\$\begingroup\$

APL (Dyalog), 30 22 20 14 bytes

Saved 6 bytes thanks to @Adám

' *'[⍉2⊥⍣¯1⊢⎕]

Try it online!

(assumes ⎕IO←0 as this is default on many machines)

This takes input as an array and returns a matrix with *s and s.

Explanation

2⊥⍣¯1⊢⎕       Convert input to binary (returns a transposed matrix of 1s and 0s)
⍉              Transpose
' *'[ ... ]    Index into this string
\$\endgroup\$
2
  • \$\begingroup\$ Save 6 bytes with ' *'[⍉2⊥⍣¯1⊢⎕] \$\endgroup\$
    – Adám
    Commented Jul 24, 2017 at 12:18
  • \$\begingroup\$ @Adám Thanks for the tips, I didn't know I could remove the ¨. \$\endgroup\$
    – user41805
    Commented Jul 24, 2017 at 13:01
4
\$\begingroup\$

PowerShell, 100 bytes

$args|%{if(($c=($a=[convert]::ToString($_,2)).length)-gt$l){$l=$c}$a-replace0,' '}|%{$_.padleft($l)}

Try it online!

Ugh, converting to binary in PowerShell is so painful. Plus .lengthy calls to -replace the 0 with spaces, plus a long .padLeft() call to make them all the same .length, all adds up to a long submission.

Golfing suggestions to get below 100 are welcome.

\$\endgroup\$
1
  • \$\begingroup\$ using: - |% Le* instead of .length - |% *ce 0 ' ' instead of -replace0,' ' - |% *ft $linstead of .padleft($l) Try it online! \$\endgroup\$
    – Julian
    Commented Nov 22, 2020 at 8:56
4
\$\begingroup\$

T-SQL, 290 bytes

declare @ int;select @=max(log(a,2))+1from @i;with t as(select convert(varchar(max),a%2)b,a/2c,@-1m,ROW_NUMBER()over(order by(select 1))r from @i union all select convert(varchar(max),concat(c%2,b))b,c/2c,m-1,r from t where m>0)select replace(b,0,' ')from t where m=0group by r,b order by r

Uses 1 for the brick piece, assumes input comes from table @

Ungolfed, with some explanation

-- assume input is presented in an input table
declare @input table (a int)
insert into @input values (15), (7), (13), (11)

---- start here

-- figure out how many characters are needed, by taking log2
declare @max int
select @max = max(log(a, 2)) + 1
from @input

-- recursive cte
-- will join against itself, recursively finding each digit in the binary string
;with cte as
(
    select 
        convert(varchar(max), a % 2) as b, -- is the least significant bit 1 or 0
        a / 2 as c, -- remove least significant bit, for the next run
        @max - 1 as max, -- keep track of iterations left
        ROW_NUMBER() over (order by (select 1)) as rn -- keep the order of the input
    from @input

    union all -- recursive loop
              -- below columns follow the same pattern

    select convert(varchar(max), 
        concat(cte.c % 2, cte.b)) as b, -- prepend the current binary string with the newest least significant bit
        cte.c / 2 as c, 
        cte.max - 1, 
        cte.rn
    from cte
    where cte.max > 0
)
select replace(b, 0, ' ') -- swap 0s for space
from cte
where max = 0 -- only take the last iteration
group by rn, b -- grab each unique input, 
               -- need to group by row number so it can be ordered by
               -- need to group by binary string, so it can be selected
order by rn -- sort by the order the input arrived in
\$\endgroup\$
4
\$\begingroup\$

Python 2, 77 75 bytes

lambda k:[[' *'[i>>y&1]for y in range(len(bin(max(k)))-3,-1,-1)]for i in k]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 40 bytes

Grid@PadLeft@IntegerDigits[#,2]/. 0->""&

Bricks are 1s

Mathematica, 48 bytes

Grid@PadLeft@IntegerDigits[#,2]/.{0->"",1->"#"}& 

Bricks are #

\$\endgroup\$
3
  • \$\begingroup\$ You should only need one slash in the //.. (/. means "replace once", //. means "keep doing the replacement until the thing stops changing".) \$\endgroup\$
    – Not a tree
    Commented Jul 24, 2017 at 11:25
  • \$\begingroup\$ You don't need the space after the comma in the IntegerDigits function. \$\endgroup\$
    – Mark S.
    Commented Jul 25, 2017 at 0:14
  • \$\begingroup\$ yes,I know,this happens when you copy/paste from notebook.fixed \$\endgroup\$
    – ZaMoC
    Commented Jul 25, 2017 at 0:17
3
\$\begingroup\$

PHP, 84 bytes

while(++$i<$argc)echo strtr(sprintf("\n%".-~log(max($argv),2).b,$argv[$i]),10,"* ");

Luckily, the bit operation casts the log result to int. float wouldn´t work here.

Try it online.

\$\endgroup\$
1
  • \$\begingroup\$ error_reporting(~E_ALL & ~E_NOTICE); is better, and php8 can't work, maybe should add version in title. \$\endgroup\$
    – LianSheng
    Commented Jan 24, 2021 at 11:32
2
\$\begingroup\$

C# (.NET Core), 112+18=130 86+41=127 bytes

a=>a.Select(n=>C.ToString(n,2).Replace("0"," ").PadLeft(C.ToString(a.Max(),2).Length))

Try it online!

The byte count includes 41 bytes from using System.Linq;using C=System.Convert;. Uses 1 as character for the wall. Nonetheless, this is way too long even for C#...

\$\endgroup\$
8
  • \$\begingroup\$ Place in namespace System.Linq{} to save some bytes. Is a.Max() guaranteed to be true (I'm sure it is I'm just not the smartest with binary :P)? Would class Convert{} save any bytes? \$\endgroup\$ Commented Jul 24, 2017 at 11:17
  • 1
    \$\begingroup\$ If I place the program in a given namespace, shouldn't I submit the whole program instead of just a lambda? I'm not sure about the rules for that... \$\endgroup\$
    – Charlie
    Commented Jul 24, 2017 at 11:23
  • \$\begingroup\$ I've usually just placed in namespace with lambda. I don't think there's ever been a question about it and it is in the C# tips page. \$\endgroup\$ Commented Jul 24, 2017 at 11:24
  • \$\begingroup\$ I don't think this is valid, as you can't compile it without using static imports. \$\endgroup\$
    – MetaColon
    Commented Jul 24, 2017 at 12:26
  • 2
    \$\begingroup\$ @MetaColon that's the reason why I added the bytes in using System.Linq;using C=System.Convert; to byte count, as those two using directives are needed for the code to compile. \$\endgroup\$
    – Charlie
    Commented Jul 24, 2017 at 12:40
2
\$\begingroup\$

Retina, 63 bytes

.+
$*#<
+`(#+)\1
$1 
 #
#
{T`<`_`^(<.+(¶|$))+$
m`^<
 <
(.)<
<$1

Try it online! Explanation:

.+
$*#<

Convert to unary, and suffix a <.

+`(#+)\1
$1 
 #
#

Convert to binary.

{T`<`_`^(<.+(¶|$))+$

Once all the <s have reached the left, delete them all.

m`^<
 <

Insert a space before any <s that have already reached the left.

(.)<
<$1

Move all the <s left one step. Rinse and repeat.

\$\endgroup\$
2
\$\begingroup\$

Clojure, 185 bytes

(fn[i](let[b(map #(Long/toBinaryString %)i)](map #(clojure.string/replace(clojure.string/replace(format(str"%0"(reduce(fn[l i](max l(count i)))0 b)"d")(read-string %))"1""#")"0"" ")b)))

Ungolfed version:

(fn [i]
    (let [b (map #(Long/toBinaryString %) i)]
        (map
            #(clojure.string/replace
                (clojure.string/replace
                    (format
                        (str "%0"
                            (reduce
                                (fn [l i] (max l(count i))) 0 b)
                            "d")
                        (read-string %))
                        "1"
                        "#")
                "0"
                " ")
        b)))

Anonymous function that takes the argument as a list. Returns the lines as list.

Reading the other answers, I bet it could be smaller. clojure.string/replace takes an obscene amount of chars to write..

\$\endgroup\$
2
\$\begingroup\$

Japt, 33 30 bytes

¡'0p(¡X¤lÃn o)-X¤l)+X¤)£" *"gX

Try it online!

Saved 3 bytes thanks to @Justin Mariner

Explanation

¡                              // map input integers
    (¡X¤lÃn o)                 // longest binary string length
              -X¤l)            // minus current binary string length
 '0p                           // repeat zero
                   +X¤)        // concat with current binary string
                       £       // map chars of binary string
                        " *"gX // swap 0 and 1 with ' ' and '*'
\$\endgroup\$
1
  • \$\begingroup\$ You can drop the last 3 chars to just return an array of strings, and use the -R flag (not added to byte count) to see the newline-joined output: here. \$\endgroup\$ Commented Jul 25, 2017 at 10:28
2
\$\begingroup\$

Python 3, 92 90 bytes

lambda a:[' '*(len(bin(max(a)))-len(i)-2)+i for i in[bin(i)[2:].replace(*'0 ')for i in a]]

Try it online!

Returns a list of lines. Stacking them up shows they do indeed align properly.

['111 ', ' 1  ', ' 11 ', '  1 ']
>>>
 111 
  1  
  11 
   1 

The breakdown

Essentially converts the array to binary, then replaces all 0's with spaces. N number of spaces are added to the front of each line where N = [length of longest line] - [length of line].

-1 bytes Thanks to Mr. Xoder

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ You can't have leading or trailing spaces in the output. \$\endgroup\$ Commented Jul 25, 2017 at 8:58
  • \$\begingroup\$ @TheLethalCoder Oh, must have misread the rules! Thanks for catching that. \$\endgroup\$
    – Graviton
    Commented Jul 25, 2017 at 9:02
  • \$\begingroup\$ 90 bytes, replace '0',' ' with *'0 '. \$\endgroup\$
    – Mr. Xcoder
    Commented Jul 25, 2017 at 10:18
  • \$\begingroup\$ @Mr.Xcoder Ah interesting, would never have thought about that. Thanks! \$\endgroup\$
    – Graviton
    Commented Jul 25, 2017 at 22:17
2
\$\begingroup\$

Japt, 11 bytes

m¤z3 z ·r0S

Try it online!

Explanation

m¤z3 z ·r0S  Implicit input of array
m¤           Map the array to binary strings
  z3 z       Rotate right 270° and then right 90°. This adds left padding to each string
       ·r0S  Join with newlines and replace 0s with spaces
\$\endgroup\$
1
  • \$\begingroup\$ Nice exploit with z3 z. Not sure why y y doesn't work there, I'll look into it later... \$\endgroup\$ Commented Jul 29, 2017 at 23:11
2
\$\begingroup\$

Java 7, 130 108 88 bytes

Saved 22 thanks to @TheLethalCoder Saved 20 thanks to @Xanderhall

void a(int[]b){for(int i:b)System.out.println(Long.toBinaryString(i).replace('0',' '));}

Ungolfed:

void a(int[]b){
    for(int i:b)
        System.out.println(Long.toBinaryString(i).replace('0', ' '));       
}
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Post increment i at b[i] to save a byte. You can keep the output with 1's so no need for the .replace('1','*'). Use Java 8 instead and compile to a lambda to save bytes. If you don't want to do that int[]b saves a byte. \$\endgroup\$ Commented Jul 25, 2017 at 12:24
  • \$\begingroup\$ Thank you! Could you please explain what "Post increment i at b[i] to save a byte." means? \$\endgroup\$ Commented Jul 25, 2017 at 12:35
  • \$\begingroup\$ i++ evaluates i then increments it (whereas ++i does the opposite) so you can move the i++ out of the for loop and use b[i++] instead. Oh and whilst we're at it you only have one line inside your loop so the braces aren't needed. \$\endgroup\$ Commented Jul 25, 2017 at 12:37
  • \$\begingroup\$ True! Amazing, thank you \$\endgroup\$ Commented Jul 25, 2017 at 12:40
  • 2
    \$\begingroup\$ You can save a few bytes by switching your loop to a foreach loop. for(int x:i) Also, you can use Long.toBinaryString instead of the Integer version to save 3 bytes. \$\endgroup\$
    – Xanderhall
    Commented Jul 26, 2017 at 14:16
2
\$\begingroup\$

PowerShell, 98 bytes

$p=$args|%{$n=$_;31..0|?{($n-shr$_)%2}}|sort
$args|%{$n=$_;-join($p[-1]..0|%{' *'[($n-shr$_)%2]})}

Try it online!

The script uses the fact that an integer contains no more than 32 bits.

The first line calculates positions of the bits with 1. The second line renders a binary wall.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 217 bytes

After 2 hours of coding i decided, that numpy is bad idea for this

import numpy as n
i=n.loadtxt("i")
o=[n.copy(i)]
o[0].fill(10)
while n.count_nonzero(i)>0:
 o.append(i%2+32)
 i=n.vectorize(lambda x:x//2)(i)
print n.fliplr(n.array(o).T).astype('uint8').view('c').tostring().decode()

Usage in Ubuntu

Install numpy

python2 -m pip install numpy

Create file named i with input in format 14 4 6 2

Run

python2 prog.py
\$\endgroup\$
1
\$\begingroup\$

8th, 232 254 250 bytes

Code

0 >r a:new swap ( nip 2 base drop >s decimal s:len r> n:max >r a:push ) a:each drop a:new swap ( nip '0 G:c# r@ G:#> s:fmt a:push ) a:each drop rdrop a:new swap ( nip /0/ " " s:replace! a:push ) a:each drop ( nip /1/ "*" s:replace! . cr ) a:each drop

Ungolfed version with comments

\ convert to binary and save longest string length
: f 0 >r a:new swap ( nip 2 base drop >s decimal s:len r> n:max >r a:push ) a:each drop ;

\ pad binary number with zero
: f1 a:new swap ( nip '0 G:c# r@ G:#> s:fmt a:push ) a:each drop rdrop ;

\ replace every 0 with space
: f2 a:new swap ( nip /0/ " " s:replace! a:push ) a:each drop ;

\ replace every 1 with * and print each line of bricks
: f3 ( nip /1/ "*" s:replace! . cr ) a:each drop ;

These words must be invoked in sequence (see example)

Usage and examples

ok> [15,7,13,11] 0 >r a:new swap ( nip 2 base drop >s decimal s:len r> n:max >r a:push ) a:each drop a:new swap ( nip '0 G:c# r@ G:#> s:fmt a:push ) a:each drop rdrop a:new swap ( nip /0/ " " s:replace! a:push ) a:each drop ( nip /1/ "*" s:replace! . cr ) a:each drop
****
 ***
** *
* **

Or more clearly

ok> [15,11,15,15] f f1 f2 f3
****
* **
****
****
\$\endgroup\$
1
\$\begingroup\$

Pyth, 16 bytes

j_MC.tm_X.Bd\0\ 

Try it here. Mind the trailing space.

\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 170 161 Bytes

Golfed

Anonymous VBE immediate window function that that takes input of format 1 2 3 .. n from range [A1] and outputs the corresponding binary wall to the VBE Immediate window via range [B1,C1,2:2]

n=Split([A1]):[A2].Resize(1,UBound(n)+1)=n:[C1]="=Int(1+Log(B1,2))":For Each i In n:[B1]=i:?Replace(Replace([Right(Rept(0,C1)&Dec2Bin(B1),C1)],1,"*"),0," "):Next

Formatted:

n=Split([A1])
[A2].Resize(1,UBound(n)+1)=n
[C1]="=Int(1+Log(B1,2))"
For Each i In n
[B1]=i
?Replace(Replace([Right(Rept(0,C1)&Dec2Bin(B1),C1)],1,"*"),0," ")
Next

Ungolfed

Full Subroutine that takes input of format Array(1, 2, 3...) and outputs the corresponding binary wall to the VBE Immediate window via range [A1,B1,2:2]

Sub a(ByRef n As Variant)
    Let Range("A1").Resize(1,UBound(n)+1) = n
    Let Range("C1").Value = "=Int(1+Log(A1,2))"
    Dim i As Integer
    For Each i In n
        Let Range("A1").Value = i
        Debug.Print Replace(
                            Replace(
                                    [Right( Rept( 0, C1) & Dec2Bin( B1), C1)],
                                    1,
                                    "*"
                            ),
                            0,
                            " "
                    )
    Next
End Sub
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 20 bytes

WS«⸿≔IιιWι«←§ *ι≧÷²ι

Try it online! Link is to verbose version of code. Works by manually converting each input number to binary but printing it in right-to-left order. I take the input as a newline terminated string as Charcoal doesn't have a good way of inputting lists otherwise I would write something like this which unfortunately currently takes 21 bytes:

WS⊞υIιW⌈υ«Eυ﹪κ²↓⸿≧÷²υ

Try it online! Link is to verbose version of code. This version vectorises over the input array, although its output is hardcoded to -s which saves a byte.

\$\endgroup\$
1
\$\begingroup\$

Japt, 7 bytes

Outputs an array of lines, using 1 for the bricks

®¤r0SÃù

Try it (footer replaces 1s with *s) or run all test cases

®¤r0SÃù     :Implicit input of array
®           :Map
 ¤          :  To binary string
  r0        :  Replace 0s with
    S       :  Space
     Ã      :End map
      ù     :Left pad each with spaces to the length of the longest
\$\endgroup\$

Your Answer

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

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