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 code-golf, so may the shortest program/function for each language win!
This comes from the sandbox.
– Charlie – 2017-07-11T06:10:52.4306It'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.8032If 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