- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Please tell me I've failed to see some crucial bit of humour in this ridiculous example.
Did they not teach you the wonders of do-while during your "proper programming education"?
Admin
Obviously, not enough. I will take a penny if you have one to spare.
Admin
Hmmm.
Two things: first, it doesn't seem to solve the task, since two ladies aren't supposed to be paired more than once.
Second, sorting @combo is never needed.
Btw: why Data::Dumper and not just print "$_\n" for keys %$threesomes?
Admin
Alex,
Seriously, I must thank you for posting this interesting problem, but also congratulate you, you clever sod. After all the whining we've seen about BYOC in the past, making this week's problem something that looks simple but quickly leads to combinatorial explosion if approached naively, was nothing short of brilliant. :D
Thank you, and keep them coming!
Admin
PHP Code:
Admin
PS I don't really have coworkers to worry about, I am usually left to handle coding all by myself. :-|
Admin
I've thought about this for a while, but I haven't managed to come up with any particularly efficient solutions yet. It's surprisingly tricky...
Admin
@Mr.'; Drop Database:
Absolutely, that's what I find brilliant about this problem. Coming up with a horribly slow or downright nonfunctional solution, as many have done here, is easy. Coming up with something that works and works well is nowhere near amateur level.
I solved it by implementing algorithm X (http://en.wikipedia.org/wiki/Knuth's_Algorithm_X) as has been suggested by other commenters, using the dancing links technique (http://en.wikipedia.org/wiki/Dancing_Links). It works well, although remains slow for num_girls=21.
The nice thing is, once you have implemented algorithm X, solving the Kirkman's Ladies problem boils down to expressing it as a boolean matrix representing an exact cover problem. It needs a little analytical thinking and possibly a piece of paper to visualize, but once you've got it you can basically solve all the (reasonably sized) problems of the same type.
This is a particularly rewarding BYOC problem, especially if you've not done deep algorithmic work in a while. I hope the upcoming ones will be as good. :)
Admin
Maybe I should debug it on just nine girls first...
Admin
Hi Mr. Drop Database,
I did it in Python too, although it's admittedly not the most efficient language for putzing around with links. I wonder if a numpy-based implementation would be thinkable.
All the same, 15925 rows sounds way too much for the N=15 problem. Even for the N=21 I 'only' have 13300 rows. For N=15 I have 3185 rows. What does your matrix look like then?
Admin
Admin
Hi Mr. DropDB (getting less and less formal here, am I?),
Yes, accounting for different positions each day is unneeded since you can trivially switch around a given day's girl triplets without impacting the validity of the solution. :)
Admin
Admin
Hi pjt33,
Nope, an NP problem stays so even given a non-naive algorithm, but those still work a hell of a lot better than naive algorithms. Remember, NP problems with a perfect theoretical heuristic can be solved in polynomial time, by definition. In practice a good heuristic -- like the selection of the column with the least 1s in algorithm X -- already makes a world of difference.
Admin
I finally got my implementation to work for n = 15 in a couple of seconds. I had to upgrade my DLX implementation to allow constraints that could be satisfied an arbitrary number of times. Even so, it seems to take an awfully long time for n = 21.
Admin
You need to be really patient to compile this. All I can say is it takes more than 4 hours for MSVC9 to compile it on Intel Core2 Duo with 2GB system memory! What's good for this is that it does all calculations in compile time so the resulting program will be super fast! Changing number_of_days constant to some value under 10 will help your compiler a lot, although you will only get results for first few days.
Q: Template metaprograms are too difficult to read. They are write-only code. A: No, come on. Look at my code! It's even quite readable without a single comment
Q: Compilation takes too much time. A: Who cares? We compile it and ship to customers. Our customers will run super-fast program and be happy.
Admin
That's a pretty clever solution. The I_CANNOT_PUT_TWO_ANGLE_BRACKETS_HERE part just floored me. :)
Admin
Admin
I lost it at this comment.
Admin
This wouldn't work in real life - girls go round in cliques and have enemies they would never talk to, so the answer is:
ABC DEF GHI JKL MNO ABC DEF GHI JKL MNO ABC DEF GHI JKL MNO ... ... ... ... ...
Admin
Admin
Your code don't run. It looks very c-ish to be ruby. And what are you trying to do with:
trip.each { |girl| print (girl + ?A).chr }; print "\n"
???
The error is on this line:
"in `+': String can't be coerced into Fixnum (TypeError)"