26
\$\begingroup\$

The other day, my son asked me to build him a staircase using Lego-ish blocks. And I came up with something like this:

Staircase

Then my kid asked me for a program using the least number of bytes that generated a similar staircase in a computer screen. I am not that good at , so I need your help. I need a program that:

  • Receives a positive integer with the number of levels the staircase needs to have.
  • Outputs a drawing of a staircase, with the pattern you see in the image.

The output will be in text format but the bricks can be distinguished one from another. For instance, you may use the '█' character as half a block and paint it in any color you want, or just choose any character of your choice.

Restrictions:

  • Blocks need to be of three different colors, which will be used as long as possible (if input is 1 or 2 there are not enough blocks to use all three colors). If you want, you may use the '░▒▓' characters, for instance, or just select three different characters.
  • No two blocks of the same color or pattern can be side to side in a single row.

My son does not really care about trailing spaces or new lines as long as a staircase is drawn.

Examples (sorry for the bad choice of characters):

Input: 1
Output:
██

Input: 2
Output:
██
 ▓▓

Input: 3
Output:
██
 ▓▓
██░░

Input: 5
Output:
██
 ██
██░░
 ██░░
██░░▓▓
\$\endgroup\$
15
  • 18
    \$\begingroup\$ Tell your child that the fun is in building / programming it himself :-) \$\endgroup\$
    – Luis Mendo
    Jun 4, 2017 at 19:51
  • 3
    \$\begingroup\$ Welcome to the site! Combining ASCII art and graphical output is usually frowned upon, as it results in sort of two parallel challenges. Also, if you go for ASCII, I would allow actual ASCII characters to be used (I think you already do, but perhaps be more explicit), as some languages may have difficulty with non ASCII characters \$\endgroup\$
    – Luis Mendo
    Jun 4, 2017 at 20:01
  • 2
    \$\begingroup\$ FYI the blocks are called Duplo \$\endgroup\$ Jun 4, 2017 at 20:10
  • 11
    \$\begingroup\$ @cairdcoinheringaahing duplo doesn't have round buttons. \$\endgroup\$
    – Christoph
    Jun 4, 2017 at 21:06
  • 3
    \$\begingroup\$ @cairdcoinheringaahing The blocks in the Question aren't Duplo, Duplo is Lego compatible, those are Lego Baby. They were originally called Duplo Primo and I wish they had a different name than either of those two, but they are technically a different product than both Lego and Duplo. \$\endgroup\$ Jun 5, 2017 at 18:37

8 Answers 8

11
\$\begingroup\$

Python 2, 55 bytes

i=2
exec"print(i%2*' '+`2%i*1122`*i)[:i];i+=1;"*input()

Try it online!

Cycles between blocks of 22, 44, except the top row is 00. For example, on input 10, prints

00
 22
2244
 2244
224422
 224422
22442244
 22442244
2244224422
 2244224422

Prints rows of increasing length i=2,3,.. by creating a space for odd lengths, repeating the pattern i times, and trunctating to length i. The pattern is 2244 for all rows except the first one i=2 for which it's 0. This is achieved with the arithmetic expression 2%i*1122.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Uses only two colors for case 3. \$\endgroup\$
    – Christoph
    Jun 4, 2017 at 20:55
  • \$\begingroup\$ Very clever fix! \$\endgroup\$
    – Charlie
    Jun 4, 2017 at 21:29
5
\$\begingroup\$

Jelly,  21 19  16 bytes

d2SR+%3x2⁶;ṙḂµ€Y

A full program printing the result.

Uses 00, 11, and 22 as the blocks.

Try it online!

How?

d2SR+%3x2⁶;ṙḂµ€Y - Main link: number n
             µ€  - for €ach "row" in n (i.e. for row, r = 1 to n inclusive):
d2               -   divmod 2   -> [integer-divide by 2, remainder] i.e. [r/2, r%2]
  S              -   sum        -> r/2 + r%2
   R             -   range      -> [1, 2, 3, ..., r/2 + r%2]
    +            -   add r      -> [r+1, r+2, r+3, ..., r + r/2 + r%2]
     %3          -   modulo 3   -> [r%3+1, r%3+2, r%3+0, ..., (r + r/2 + r%2)%3]
                 -   e.g.: r: 1  , 2  , 3    , 4    , 5      , 6      , 7       , ...
                             [2], [0], [1,2], [2,0], [0,1,2], [1,2,0], [2,0,1,2], ...
       x2        -   times 2 - repeat each twice (e.g. [2,0,1,2] -> [2,2,0,0,1,1,2,2]
         ⁶       -   literal space character
          ;      -   concatenate (add a space character to the left)
            Ḃ    -   r mod 2 (1 if the row is odd, 0 if it is even (1 at the top))
           ṙ     -   rotate (the list) left by (r mod 2)
               Y - join with newlines
                 - implicit print (no brackets printed due to the presence of characters)
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 80 bytes

n=>eval(`for(s=11,i=1;i++<n;)s+='\\n'+(' '+'2233'.repeat(n)).substr(i%2,i+1);s`)

f=
n=>eval(`for(s=11,i=1;i++<n;)s+='\\n'+(' '+'2233'.repeat(n)).substr(i%2,i+1);s`)
console.log(f(+prompt()))


JavaScript (ES6), 87 bytes

Previous solution.

n=>[11,...Array(n).fill(' '+'2233'.repeat(n)).map((r,n)=>r.slice(n%2,n+3+n%2))].join`
`
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 22 21 20 18 17 bytes

Uses the interesting fact that 4^(N+2)/5 = [3,12,51,204,...] = b[11,1100,110011,11001100,...]

F4NÌm5÷bDðì}r·IF,

Try it online!

Explanation

F                     # for N in 0...input-1 do
 4                    # push 4
  NÌ                  # push N+2
    m                 # push 4^(N+2)
     5÷               # integer division by 5
       b              # convert to binary
        D             # duplicate
         ðì           # prepend a space to the copy
           }          # end loop
            r         # reverse stack
             ·        # multiply top of stack by 2
              IF      # input times do
                ,     # print with newline
\$\endgroup\$
2
  • \$\begingroup\$ Sorry, I improved my answer - nice method though! \$\endgroup\$ Jun 16, 2017 at 9:36
  • \$\begingroup\$ @JonathanAllan: Of course you did ;) Thanks! Pretty cool idea of handling the indentation with mod2 rotations in your answer. \$\endgroup\$
    – Emigna
    Jun 16, 2017 at 11:16
3
\$\begingroup\$

SOGL, 31 28 27 25 bytes

∫³2\@*O"²b“2⁵I%*r*;I»«nKp

Explanation:

∫                          iterate input times, pushing 1-indexed counter
 ³                         get 3 total copies of it on stack
  2\                       1 if divides by 2, else 0
    @*                     get that many spaces
      O                    output in a new line
       "²b“                push 1122
           2⁵I%*           multiply 1122 by 2%(iteration+1)
                r          convert to string
                 *         multiply by iteration
                  ;I»«     get one iteration variable ontop of stack
                      n    increase, floor divide by 2, multiply by 2 (gets the amount of bricks in a line)
                       Kp  split that multiplied string in pieces of that length

using this technique
Example output for 9:

00
 22
2244
 2244
224422
 224422
22442244
 22442244
2244224422

22 bytes

∫³2\@*O"²b“2⁵I%*;I»«mp

The command m has been documentated on the 1st SOGL commit, just not implemented.

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

PHP, 61 59

aa<?for(;++$i<$argn;)echo"
",str_pad(" "[~$i&1],2+$i,bbcc);

works pretty much like the python versions but uses all three colors if possible. No trailing new line.

-2 bytes by @user63956. Thanks !
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can save two bytes with aa<?for.... \$\endgroup\$
    – user63956
    Jun 5, 2017 at 2:17
1
\$\begingroup\$

Pyth, 29 bytes

VQI!%hN2pd)Vh/N2p*2@G%+NH3)pb

Test it online!

Explanations

VQI!%hN2pd)Vh/N2p*2@G%+NH3)pb

VQ                               For N in range(0, input)
  I!%hN2pd)                      If N is odd, print a leading space
           Vh/N2          )      For H in range(0, N / 2 + 1)
                   @G%+NH3       Select the letter at position (N + H) % 3 in the alphabet
                 *2              Then make it a two letters string ("aa" or "bb" or "cc")
                p                Print it
                           pb    End the line by printing a new line

I am sure there are a lot of ways to shorten that code, but I am king of tired right now... Will try later.

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

Batch, 125 bytes

@set s=█
@for /l %%i in (2,1,%1)do @call:c
:c
@set s= %s:█= %
@set s=%s:▓=█%
@set s=%s:░=▓%
@set s=%s:  =░░%
@echo %s%

Note: Save this in CP437 or CP850 or some such. Works by rotating the colours each time. Since I can't map over the string to perform the rotation, I use four replacements, using spaces as a temporary stage. This then also allows me to prefix a space every line, so that two spaces turn into a new block. Sample output:

░░
 ▓▓
░░██
 ▓▓░░
░░██▓▓
\$\endgroup\$

Your Answer

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

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