House of cards (version 2)

8

1

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!

Charlie

Posted 2017-07-11T06:08:08.270

Reputation: 11 448

This comes from the sandbox.

– Charlie – 2017-07-11T06:10:52.430

6It's an interesting challenge but personally I think you should have waited a bit longer, maybe a day, between posting them. – caird coinheringaahing – 2017-07-11T06:17:53.247

@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 – 2017-07-11T06:23:12.123

Surely /\\ is a tallest possible house with 5 cards? – Peter Taylor – 2017-07-11T06:50:11.703

@PeterTaylor no, it isn't. It's /\/\\ with a bridge card over them. – Charlie – 2017-07-11T06:52:48.803

2If the width of a bridge card is nonzero, shouldn't the output for 1 card be a flat card? – Peter Taylor – 2017-07-11T06:57:02.643

@PeterTaylor I didn't think of that, that's a good point. Question updated. – Charlie – 2017-07-11T07:00:47.287

2I 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 – 2017-07-11T07:37:59.967

@PeterTaylor well, that's why I added an explanation about the way how houses of cards are built. So when I say "the tallest house of cards" I mean "the tallest you can following the building way I mention further in the question". The case of having only one card was a particular case I did not think of before, but I'll leave the rest of test cases as they are. – Charlie – 2017-07-11T07:42:31.033

I have reworded the question hoping it is now clearer that the building order is more important than just "build it as tall as you can". – Charlie – 2017-07-11T09:21:22.077

But with 3 cards, wouldn't it be /\--, seeing as how one card is simply --? – zgrep – 2017-07-13T03:47:03.853

Answers

4

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

Neil

Posted 2017-07-11T06:08:08.270

Reputation: 95 035

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 – 2017-07-11T09:51:33.943

@CarlosAlejo No, I'd just forgotten about that case. Sorry about that. Fixed now. – Neil – 2017-07-11T10:08:49.277

1@Neil are you sure you didn't make charcoal? Cause I'm starting to not believe you. – Magic Octopus Urn – 2017-07-11T21:41:39.133

@MagicOctopusUrn D: hey (but yeah Neil's Charcoal skills are amazing, probably even better than mine haha) – ASCII-only – 2017-07-27T02:15:58.233

Oops sorry, will be fixed in next commit – ASCII-only – 2017-07-27T03:09:26.113

@ASCII-only I see that the »« is no longer necessary in succinct mode, thanks, but I still can't get Verbose mode to accept a switch... – Neil – 2017-07-27T09:30:24.180

2

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

Coty Johnathan Saxman

Posted 2017-07-11T06:08:08.270

Reputation: 280

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 – 2017-07-11T08:34:21.060

Let us continue this discussion in chat.

– Coty Johnathan Saxman – 2017-07-11T10:45:40.527

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 – 2017-07-12T09:32:07.523

@TaylorScott I tried changing to <pre><code>... but it seemed to cancel the language-all: python tag's effect. Highlighting went haywire, spacing was ruined, random italics entered the code, and, most confusingly, some chains of characters were swapped between code blocks...? I did manage to reformat the post, though, with 4-space indents mixed with no indent lines. Hopefully this is easier to read. – Coty Johnathan Saxman – 2017-07-13T02:28:15.357

Just to follow up with this, the lines you would need to use to separate the code would look something like <pre><code>f=lambda ... #function 1, d=lambda ..., </code></pre>, <pre><code>f=lambda ... #function 2, d=lambda ..., </code></pre> (where the lines are comma delineated) and note that you will have to wait a short period of time for the language flag to update the highlighting (approx 5-10 seconds after you have stopped typing edits) – Taylor Scott – 2017-07-13T12:58:58.263

@TaylorScott unless you mean to actually put commas into the code (and I don't think that's what you mean), that's exactly how I tried to do it... I waited for code highlighting, and when it kicked in, everything went crazy. It seemed to 'forget' that it was looking at Python, and formatted it for C? Maybe Java?

I invite you to suggest an edit if you think it would improve readability, or if you're inclined to show me how it works. If it's just a matter of ease-of-input, the indents don't particularly bother me (in Python, they come with the territory as it is...). – Coty Johnathan Saxman – 2017-07-14T02:39:08.847

1I 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 – 2017-07-14T12:12:23.023

2@TaylorScott A little late but you can fix it by replacing them with HTML entities: &lt; for < and &gt; for > – Business Cat – 2017-08-25T16:01:00.990

1

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!

Xcali

Posted 2017-07-11T06:08:08.270

Reputation: 7 671