# House of cards (version 2)

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!

• This comes from the sandbox. – Charlie Jul 11 '17 at 6:10
• It's an interesting challenge but personally I think you should have waited a bit longer, maybe a day, between posting them. – caird coinheringaahing Jul 11 '17 at 6:17
• @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. – Charlie Jul 11 '17 at 6:23
• If the width of a bridge card is nonzero, shouldn't the output for 1 card be a flat card? – Peter Taylor Jul 11 '17 at 6:57
• 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). – Peter Taylor Jul 11 '17 at 7:37

# Charcoal, 67 bytes

Ｎθ⁼θ¹Ａ²ηＷ¬‹θη«←÷η³↓→/…\/÷η³↙Ａ⁻θηθＡ⁺³ηη»‖ＭＭ÷η³→Ｆθ≡﹪ι³¦¹«↗←\/»²«↑Ｐ²»«


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

Ｎθ


Read the input as an integer into θ.

⁼θ¹


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

Ａ²ηＷ¬‹θη«


η 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.)

Ａ⁻θηθＡ⁺³ηη»


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

‖Ｍ


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

Ｍ÷η³→


Move the cursor to the right of the house.

Ｆθ


Repeat for each remaining card (if any).

≡﹪ι³¦


If the card modulo 3 is:

¹«↗←\/»


1, then print a pair of cards;

²«↑Ｐ²»


2, then print a horizontal card;

«


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

• 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... – Charlie Jul 11 '17 at 9:51
• @CarlosAlejo No, I'd just forgotten about that case. Sorry about that. Fixed now. – Neil Jul 11 '17 at 10:08
• @Neil are you sure you didn't make charcoal? Cause I'm starting to not believe you. – Magic Octopus Urn Jul 11 '17 at 21:41
• @MagicOctopusUrn D: hey (but yeah Neil's Charcoal skills are amazing, probably even better than mine haha) – ASCII-only Jul 27 '17 at 2:15
• Oops sorry, will be fixed in next commit – ASCII-only Jul 27 '17 at 3:09

# 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
• 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. – Charlie Jul 11 '17 at 8:34
• – Coty Johnathan Saxman Jul 11 '17 at 10:45
• 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 – Taylor Scott Jul 12 '17 at 9:32
• 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 – Taylor Scott Jul 14 '17 at 12:12
• @TaylorScott A little late but you can fix it by replacing them with HTML entities: &lt; for < and &gt; for > – Business Cat Aug 25 '17 at 16:01

# 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!