21
5
Mash Up Time!
This is instalment #5 of both my Random Golf of the Day and Optimizer's ASCII Art of the Day series. Your submission(s) in this challenge will count towards both leaderboards (which you can find the linked posts). Of course, you may treat this like any other code golf challenge, and answer it without worrying about either series at all.
Hole 5: Diamond Tilings
A regular hexagon can always be tiled with diamonds like so:
We will use an ASCII art representation of these tilings. For a hexagon of side-length 2, there are 20 such tilings:
____ ____ ____ ____ ____ ____ ____ ____ ____ ____
/\_\_\ /\_\_\ /\_\_\ /\_\_\ /_/\_\ /_/\_\ /\_\_\ /_/\_\ /_/\_\ /_/\_\
/\/\_\_\ /\/_/\_\ /\/_/_/\ /\/_/\_\ /\_\/\_\ /\_\/_/\ /\/_/_/\ /\_\/\_\ /\_\/_/\ /_/\/\_\
\/\/_/_/ \/\_\/_/ \/\_\_\/ \/_/\/_/ \/\_\/_/ \/\_\_\/ \/_/\_\/ \/_/\/_/ \/_/\_\/ \_\/\/_/
\/_/_/ \/_/_/ \/_/_/ \_\/_/ \/_/_/ \/_/_/ \_\/_/ \_\/_/ \_\/_/ \_\/_/
____ ____ ____ ____ ____ ____ ____ ____ ____ ____
/_/_/\ /\_\_\ /_/\_\ /_/_/\ /_/\_\ /_/\_\ /_/_/\ /_/_/\ /_/_/\ /_/_/\
/\_\_\/\ /\/_/_/\ /_/\/_/\ /\_\_\/\ /\_\/_/\ /_/\/_/\ /_/\_\/\ /\_\_\/\ /_/\_\/\ /_/_/\/\
\/\_\_\/ \/_/_/\/ \_\/\_\/ \/_/\_\/ \/_/_/\/ \_\/_/\/ \_\/\_\/ \/_/_/\/ \_\/_/\/ \_\_\/\/
\/_/_/ \_\_\/ \_\/_/ \_\/_/ \_\_\/ \_\_\/ \_\/_/ \_\_\/ \_\_\/ \_\_\/
Given a side length N
, you should generate such a tiling for a hexagon of side length N
at random. The exact distribution doesn't matter, but each tiling must be returned with non-zero probability.
For N ≤ 4
, your submission must produce a tiling within 1 minute at least 80% of the time and at least 80% of the tilings must potentially be generated within 1 minute. Most approaches won't have to worry about this rule (it's very lenient) - this is just to rule out very naive rejection-based algorithms that generate arbitrary strings until one happens to be a tiling.
You might like to know that the total number of possible tilings for given N can be found in OEIS A008793.
You may write a full program or a function and take input via STDIN (or closest alternative), command-line argument or function argument and produce output via STDOUT (or closest alternative), function return value or function (out) parameter.
You must not output more leading spaces than necessary to align the hexagon (that is the left corner of the hexagon should have no spaces in front of it). Each line may contain up to N
trailing spaces (not necessarily consistently, so you could e.g. have a rectangular output, printing the hexagon's bounding box).
This is code golf, so the shortest submission (in bytes) wins. And of course, the shortest submission per user will also enter into the overall leaderboard of the series.
Leaderboards
The first post of each series generates a leaderboard.
To make sure that your answers show up, please start every answer with a headline, using the following Markdown template:
# Language Name, N bytes
where N
is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
# Ruby, <s>104</s> <s>101</s> 96 bytes
(The language is not currently shown, but the snippet does require and parse it, and I may add a by-language leaderboard in the future.)
3Is it just me who keeps seeing the example image in 3D? – LegionMammal978 – 2015-06-03T22:06:39.017
3@LegionMammal978 No that's perfectly normal. ;) (And is probably also a good way to approach the challenge.) – Martin Ender – 2015-06-03T22:07:11.680
For N ≤ 4, your submission must produce a tiling within 1 minute at least 80% of the time.
too easy: 80% of time the same, basic tiling, otherwise I find another tiling in whatever time I want – edc65 – 2015-06-04T16:14:01.937@edc65 Good point, let me rephrase that. – Martin Ender – 2015-06-04T16:15:59.160