Interestingly, it does not run much faster than a simple algorithm I wrote from scratch. For the rack size of 5, both run around 5m30sec on my box.
Update: The code is buggy. See Re^3: Wordfeud racks for a fix.
#!/usr/bin/perl
use warnings;
use strict;
use 5.010; # defined-or, say
my @tiles = (('_') x 2, # 2 blank tiles (scoring 0 points)
# 1 point
('E') x 12,
qw(A I) x 9,
('O') x 8,
qw(N R T) x 6,
qw(L S U) x 4,
# 2 points
('D') x 4,
('G') x 3,
# 3 points
qw(B C M P) x 2,
# 4 points
qw(F H V W Y) x 2,
# 5 points
('K'),
# 8 points
qw(J X),
# 10 points
qw(Q Z));
my $rack_size = 5;
my %hash;
my @rack = 0 .. $rack_size - 1;
while ($rack[-1] != @tiles) {
my $string = join q(), map $tiles[$_], @rack;
$hash{$string}++;
my $first_to_move = 0;
$first_to_move++ while $rack[$first_to_move] + 1 == ($rack[$first_
+to_move + 1] // -1);
$rack[$first_to_move]++;
for my $i (0 .. $first_to_move - 1) {
$rack[$i] = $i;
}
}
my $combinations = keys %hash;
my $sum = 0;
$sum += $_ for values %hash;
print map "$_ => " . ($hash{$_} / $sum) . "\n", sort { $hash{$a} <=> $
+hash{$b} } keys %hash;
print "Combinations: $combinations, Count: $sum.\n";
With the module, the important part of the code simplifies to:
use Algorithm::Combinatorics qw(combinations);
my %hash;
my $iterator = combinations(\@tiles, $rack_size);
while (my $rack = $iterator->next) {
$hash{ join q(), @$rack }++;
}
Update: I did some benchmarks. It seems the module would get faster than my code on racks greater than 5, but it will still run for days.
1 =================
Rate module simple
module 2953/s -- -24%
simple 3883/s 31% --
2 =================
Rate module simple
module 59.1/s -- -13%
simple 67.9/s 15% --
3 =================
Rate module simple
module 1.65/s -- -9%
simple 1.81/s 10% --
4 =================
(warning: too few iterations for a reliable count)
(warning: too few iterations for a reliable count)
s/iter module simple
module 15.5 -- -2%
simple 15.2 2% --
5 =================
(warning: too few iterations for a reliable count)
(warning: too few iterations for a reliable count)
s/iter module simple
module 328 -- -0%
simple 327 0% --
Update 2: Fixed a bug in the code.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.