10
Task
Write a program that reads three integers m, n either from STDIN or as command-line arguments, prints all possible tilings of a rectangle of dimensions m × n by 2 × 1 and 1 × 2 dominos and finally the number of valid tilings.
Dominos of an individual tiling have to be represented by two dashes (-
) for 2 × 1 and two vertical bars (|
) for 1 × 2 dominos. Each tiling (including the last one) has to be followed by a linefeed.
For scoring purposes, you also have to accept a flag from STDIN or as command line argument that makes your program print only the number of valid tilings, but not the tilings itself.
Your program may not be longer than 1024 bytes. It has to work for all inputs such that m × n ≤ 64.
(Inspired by Print all domino tilings of 4x6 rectangle.)
Example
$ sdt 4 2
----
----
||--
||--
|--|
|--|
--||
--||
||||
||||
5
$ sdt 4 2 scoring
5
Scoring
Your score is determined by the execution time of your program for the input 8 8 with the flag set.
To make this a fastest code rather than a fastest computer challenge, I will run all submissions on my own computer (Intel Core i7-3770, 16 GiB PC3-12800 RAM) to determine the official score.
Please leave detailed instructions on how to compile and/or execute your code. If you require a specific version of your language's compiler/interpreter, make a statement to that effect.
I reserve the right to leave submissions unscored if:
There is no free (as in beer) compiler/interpreter for my operating system (Fedora 21, 64 bits).
Despite our efforts, your code doesn't work and/or produces incorrect output on my computer.
Compilation or execution take longer than an hour.
Your code or the only available compiler/interpreter contain a system call to
rm -rf ~
or something equally fishy.
Leaderboard
I've re-scored all submissions, running both compilations and executions in a loop with 10,000 iterations for compilation and between 100 and 10,000 iterations for execution (depending on the speed of the code) and calculating the mean.
These were the results:
User Compiler Score Approach
jimmy23013 GCC (-O0) 46.11 ms = 1.46 ms + 44.65 ms O(m*n*2^n) algorithm.
steveverrill GCC (-O0) 51.76 ms = 5.09 ms + 46.67 ms Enumeration over 8 x 4.
jimmy23013 GCC (-O1) 208.99 ms = 150.18 ms + 58.81 ms Enumeration over 8 x 8.
Reto Koradi GCC (-O2) 271.38 ms = 214.85 ms + 56.53 ms Enumeration over 8 x 8.
Why not make this a GOLF contest? :( – orlp – 2015-05-31T14:36:23.080
No, I mean a GOLF challenge.
– orlp – 2015-05-31T15:06:37.3772If you had suggested that in the sandbox, I might have. That would have saved my CPU and me a lot of work... – Dennis – 2015-05-31T15:11:14.607
@user23013: No, the results do not have to be sorted. I'll edit the question. – Dennis – 2015-05-31T16:19:05.483
I'm slightly confused. What I got is that every domino type (
-
or|
) must appear at least twice. But then why is there no-||-
? Or is it that the side-dominos (-
) must always appear consecutively? Some more examples would be nice, too... – kirbyfan64sos – 2015-05-31T18:03:27.0403@kirbyfan64sos The way I understand it, there's only one type of domino, which you can rotate. If it's horizontal, it looks like this:
--
. If it's vertical, it is two|
, one below the other. – Reto Koradi – 2015-05-31T18:41:22.797@RetoKoradi That makes sense. Thanks! – kirbyfan64sos – 2015-05-31T19:18:05.640
isn't this going to be I/O-limited? – feersum – 2015-05-31T20:32:17.933
@feersum: Possibly. I assumed this would take a lot longer. Shows how much I know about writing fast code. :/ – Dennis – 2015-05-31T20:39:42.870
@feersum I'm worried about that, too. Redirecting to /dev/null will cut out part of the I/O overhead. But you still have to generate strings, and it seems likely that this could take much longer than finding the solutions. An alternative might have been to only count the solutions, without having to output them. – Reto Koradi – 2015-05-31T20:58:25.820
Do we have to check for the case where
m*n
is odd? E.g. for9x7
, there are no solutions. – Reto Koradi – 2015-05-31T23:36:35.150@RetoKoradi: Your code has to work for odd mn, which means it should print nothing. – Dennis – 2015-05-31T23:39:21.950
Unfortunately our concerns were correct. My initial implementation uses almost 99% of the time for output. Finding the solutions for the 8x6 size is under 10 ms on a modest laptop. At least if I got it right... Do you want to share how many solutions there are? Does the number start with a 1 and end with 89? – Reto Koradi – 2015-06-01T01:01:27.603
@Dennis If I were you I would re-post this challenge using the GOLF scoring method - right now it's kinda useless since it just becomes an I/O contest. – orlp – 2015-06-01T04:56:07.553
@RetoKoradi: As you already know by now, there are 167089 tilings for 8x6. Sorry for not answering earlier. – Dennis – 2015-06-01T05:00:33.047
@orlp: Given how unfamiliar I am with GOLF (and assembly in general, I might add), I'm not sure I should be the one to do that. At least not right now. You're more than welcome to though. – Dennis – 2015-06-01T05:13:36.323
1Your challenge is not bad. The problem is that our top coders are too strong. My solution that checks validity of rows and columns stays near 1 minute for 6x8. – edc65 – 2015-06-02T07:00:46.440
1I think the best strategy now is to use assembly, and try to get a binary file less than 1024 bytes, to get rid of the complication time. – jimmy23013 – 2015-06-04T07:44:15.990