8
\$\begingroup\$

Thanks to FryAmTheEggman for the idea for this second version.
Version 1 here.

Not-so-simple challenge: given a number of cards, build the biggest house of cards you can with that number of cards, according to the following building order:

                          /\       /\         /\         /\/\
                 --       --       --         ----       ----
/\  →  /\/\  →  /\/\  →  /\/\  →  /\/\/\  →  /\/\/\  →  /\/\/\  →

                /\         /\
     --         --         --
    /\/\       /\/\       /\/\
    ----       ----       ----
→  /\/\/\  →  /\/\/\  →  /\/\/\/\  →  ...

So you start with a single-storey house, then build the adjacent group, then put the bridge card, then build the group in the second floor, then start building groups and bridges from the first floor diagonally to reach the third floor, and so on.

A single card will be represented with a /, a \ or a --. If after using as many cards as possible you have one card left, just output what you have accomplished so far (see example for 3 cards, the result is the same as for 2 cards). The only exception is the case of 1 card, that must output a flat card.

Examples:

Input: 1
Output: 

--   <a card lying on the floor>

Input: 2
Output:

/\

Input: 3
Output:

/\

Input: 5
Output:

 --
/\/\

Input: 10
Output:

 /\
 ----
/\/\/\

Input: 20
Output:

  /\
  --
 /\/\/\
 ------
/\/\/\/\

Input: 39
Output:

    --
   /\/\
   ----
  /\/\/\
  ------
 /\/\/\/\
 --------
/\/\/\/\/\

Input: 40
Output:

    /\
    --
   /\/\
   ----
  /\/\/\
  ------
 /\/\/\/\
 --------
/\/\/\/\/\

Input can be numeric or a string, and will always be a positive integer. Output must be exactly as shown, with leading and trailing spaces and newlines allowed.

This is , so may the shortest program/function for each language win!

\$\endgroup\$
11
  • \$\begingroup\$ This comes from the sandbox. \$\endgroup\$
    – Charlie
    Commented Jul 11, 2017 at 6:10
  • 6
    \$\begingroup\$ It's an interesting challenge but personally I think you should have waited a bit longer, maybe a day, between posting them. \$\endgroup\$ Commented Jul 11, 2017 at 6:17
  • \$\begingroup\$ @cairdcoinheringaahing I admit that I did not know how much time I should have waited between both posts. It's been a while since the last answer for version 1, so I supposed I could post it now (24 hours after). Thanks for your advice, I'll bear that in mind next time. \$\endgroup\$
    – Charlie
    Commented Jul 11, 2017 at 6:23
  • 2
    \$\begingroup\$ If the width of a bridge card is nonzero, shouldn't the output for 1 card be a flat card? \$\endgroup\$ Commented Jul 11, 2017 at 6:57
  • 2
    \$\begingroup\$ I think half of the test cases are now wrong, because they're not taking into account the possibility of putting a flat layer on the bottom. The sandbox doesn't really work unless you leave a question in there for a few days so that people have time to comment. (And for "part 2"s it's important to make it clear that it's a part 2 so that people don't think it's part 1 and they've already seen it). \$\endgroup\$ Commented Jul 11, 2017 at 7:37

3 Answers 3

4
\$\begingroup\$

Charcoal, 67 bytes

Nθ⁼θ¹A²ηW¬‹θη«←÷η³↓→/…\/÷η³↙A⁻θηθA⁺³ηη»‖MM÷η³→Fθ≡﹪鳦¹«↗←\/»²«↑P²»«

Try it online! Note: The latest version of Charcoal doesn't need the »« for -2 bytes. Explanation:

Nθ

Read the input as an integer into θ.

⁼θ¹

Special case: if the input is 1, print a -.

A²ηW¬‹θη«

η represents the number of cards needed to build the next layer, initially 2. A while loop repeats as long as there are enough cards for the layer.

←÷η³↓→/…\/÷η³↙

Print the left half of the next layer. (I wanted to print the right half, but I couldn't get it to reflect properly for some reason.) The number of -s is one third of the number of cards in the layer, rounded down. (See also my answer to part 1.)

A⁻θηθA⁺³ηη»

Subtract the number of cards from the input number, and add three cards to the number required for the next layer.

‖M

Mirror the house so far. (This also turns the - into -- for the case of 1 card.)

M÷η³→

Move the cursor to the right of the house.

Fθ

Repeat for each remaining card (if any).

≡﹪鳦

If the card modulo 3 is:

¹«↗←\/»

1, then print a pair of cards;

²«↑P²»

2, then print a horizontal card;

«

Otherwise skip the card (because a pair is needed at this point).

\$\endgroup\$
6
  • \$\begingroup\$ Very nice answer! Did you start to write it before I changed the case of 1 card? That case must output -- (a flat card), but if you started your program before I changed that requirement I'll skip that... \$\endgroup\$
    – Charlie
    Commented Jul 11, 2017 at 9:51
  • \$\begingroup\$ @CarlosAlejo No, I'd just forgotten about that case. Sorry about that. Fixed now. \$\endgroup\$
    – Neil
    Commented Jul 11, 2017 at 10:08
  • 1
    \$\begingroup\$ @Neil are you sure you didn't make charcoal? Cause I'm starting to not believe you. \$\endgroup\$ Commented Jul 11, 2017 at 21:41
  • \$\begingroup\$ @MagicOctopusUrn D: hey (but yeah Neil's Charcoal skills are amazing, probably even better than mine haha) \$\endgroup\$
    – ASCII-only
    Commented Jul 27, 2017 at 2:15
  • \$\begingroup\$ Oops sorry, will be fixed in next commit \$\endgroup\$
    – ASCII-only
    Commented Jul 27, 2017 at 3:09
2
\$\begingroup\$

Python 2, 167 182 bytes

167 bytes

f=lambda x,y=0:y<x<2and"--"or~-x>y*3and f(x-2-y*3,y+1)or d(y,x)
d=lambda h,r,v=0:h and d(~-h,max(r-3,1),-~v)+" "*-~v+"--"*(h-(r<3))+"\n"+" "*v+"/\\"*(h+(r>1))+"\n"or""

182 bytes

f=lambda x,y=0:[x>1+y*3and f(x-2-y*3,y+1)or d(y,x),"--"][2>x>y]
d=lambda h,r,v=0:d(h-1,r-3*(r>2)-2*(r==2),v+1)+" "*(v+1)+"--"*(h-1+(r>2))+"\n"+" "*v+"/\\"*(h+(r>1))+"\n"if h+r else""

Explanation (167-byte version)

f=lambda x,y=0:
    "--" if y<x<2         # handle case where x is 1 on step 0 (only one card) 
    else f(x-2-y*3,y+1)   # recursive call with one full triangular level accounted for
    if x>= 2+y*3          # one full level requires 2+3y cards (2, 7, 15...)
    else d(y,x)           # if no more full levels can be constructed, draw
d=lambda h,r,v=0:         # (h)eight to draw, (r)emaining cards, (v)ertical height already drawn (to determine leading white space)
    d(h-1,          ,v+1) # recursive call to draw upper lines
          max(r-3,1)      # subtract remainder cards used in this iteration
    +" "*(v+1)            # leading whitespace for -- row
    +"--"*(       )+"\n"  # -- line. 
           h-(r<3)        # horizontal card count is equal to the remaining count of levels to draw, minus 1, 
                          # ...plus 1 if there are at least three remaining cards to add to the right
    +" "*v                # leading whitespace for /\ row
    +"/\\"*(       )+"\n" # /\ line
            h+(r>1)       # vertical card pair count equals remaining level count
                          # ...plus 1 if there are at least two extra cards
    if h                  # return above is there are levels to draw (h) 
    else ""               # else return nothing (ends recursion)

Try it online!

  • This feels unnecessarily long, but I can't see anything to remove at the moment...feel free to comment any suggestions
\$\endgroup\$
8
  • \$\begingroup\$ Thank you very much for you answer, but your examples do not match the test cases, you put more cards in your houses than you have. For instance, in the case of 7 cards you can build a house with 4 cards on the first floor, a bridge card and 2 more cards on the second floor. But you show a house with 10 cards (6 cards in the first floow and 2 bridge cards). Check the examples and the way the houses are built. \$\endgroup\$
    – Charlie
    Commented Jul 11, 2017 at 8:34
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ Commented Jul 11, 2017 at 10:45
  • \$\begingroup\$ If you want to separate the sections of code that directly follow one another you can do so by using the <pre><code>... </code></pre> tags around your code individual blocks rather indenting 4 spaces \$\endgroup\$ Commented Jul 12, 2017 at 9:32
  • 1
    \$\begingroup\$ I see what has happened: for some reason when you are using the <pre><code> tags it is capturing <3))+"\n"+" "*v+"/\\"*(h+(r> as another tag and not displaying that region of the solution. Perhaps someone in the community who understands this a little better could inform us as how to avoid that... but aside from that your current formatting is perfectly readable and therefore negates the need for this type of formatting all together \$\endgroup\$ Commented Jul 14, 2017 at 12:12
  • 2
    \$\begingroup\$ @TaylorScott A little late but you can fix it by replacing them with HTML entities: &lt; for < and &gt; for > \$\endgroup\$ Commented Aug 25, 2017 at 16:01
1
\$\begingroup\$

Perl 5, 129 bytes

@r=($_=<>-2)<0?'--':'/\\';while($_>1){$#r+=2;$i=0;$r[$i].=$i++%2?'--':'/\\'while$i<@r&&($_-=2-$i%2)>=0}say$"x(@r/2).pop@r while@r

Try it online!

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