Definitive answers...
Jan. 11th, 2014 10:19 amMy slumber was not visited by the faint echoes of sugarplum fairies, heading back to wherever it is they go once the holiday season has waned and, to borrow a phrase from Harry Harrison, existence has largely returned to the standard "bowb-your-buddy" brouhaha of everyday life. No, I kept thinking about these farblegargling dice. And about how to modify the code I wrote yesterday to get some definitive answers.
Which brought to mind a very early class assignment from college days—something like the first or second, if memory serves—where our goal was to write a program that would accept seven two-character representations of playing cards (e.g., [9S, TS, 2S, AC, AS, AH, 2S]) and output the name of the best five-card poker hand that could be assembled from the input cards (a flush, in this instance). I still remember just how difficult it was to approach the problem, until I realized that just because I thought a particular process was cumbersome, that didn't mean it was cumbersome for a computer to perform. Once I got that through my skull, the rest was relatively easy. But I digress...
So upon rising this morning, I wrote a Perl subroutine—one day, I shall have to tell you about just what I've forgotten about doing that—whose purpose was to replace a roll (represented by, say, 112444) with the constituent groupings of said roll. For this particular roll, for example, two 1s, one 2, and three 4s would be represented by 213. This grouping gets sorted (so that different combinations of groupings, e.g., 123, 213, 321, etc., all end up looking the same) and output to stdout, which I redirected to a file I called allcombs.txt. The modified code looks like this:
So now, all I have to do to scratch that itch is figure out how to get these numbers, um, analytically. (Technically, I've already done so for grouping 6, 5+1, 4+2, and 1+1+1+1+1+1. The rest? I'm missing something. Worse, since I'm using the same approach to get both right and wrong answers, maybe I'm getting my right answers... by accident?)
But I think I have quieted my inner geek, for the moment. There are other things to do, and daylight is burning!
Which brought to mind a very early class assignment from college days—something like the first or second, if memory serves—where our goal was to write a program that would accept seven two-character representations of playing cards (e.g., [9S, TS, 2S, AC, AS, AH, 2S]) and output the name of the best five-card poker hand that could be assembled from the input cards (a flush, in this instance). I still remember just how difficult it was to approach the problem, until I realized that just because I thought a particular process was cumbersome, that didn't mean it was cumbersome for a computer to perform. Once I got that through my skull, the rest was relatively easy. But I digress...
So upon rising this morning, I wrote a Perl subroutine—one day, I shall have to tell you about just what I've forgotten about doing that—whose purpose was to replace a roll (represented by, say, 112444) with the constituent groupings of said roll. For this particular roll, for example, two 1s, one 2, and three 4s would be represented by 213. This grouping gets sorted (so that different combinations of groupings, e.g., 123, 213, 321, etc., all end up looking the same) and output to stdout, which I redirected to a file I called allcombs.txt. The modified code looks like this:
my @result = ();Then, just as I did yesterday with rolls, I used the command line to sort the combinations of groupings in allcombs.txt and pipe the result through uniq -c to a file I called uniquecombs.txt, wherein I got The Answer (or something like it):
my @roll = ();
sub analyze {
my $in = $_[0];
$next_in = pop(@roll);
if ($in == $next_in) {
if (@result) {
$increment = pop(@result);
} else {
$increment = 0;
}
$increment++;
push(@result, $increment);
} else {
push(@result, 1);
}
if (@roll) {
analyze($next_in);
} else {
@result = sort @result;
print(@result,"\n");
@result = ();
}
}
for ($d1 = 1; $d1 <= 6; $d1++) {
for ($d2 = 1; $d2 <= 6; $d2++) {
for ($d3 = 1; $d3 <= 6; $d3++) {
for ($d4 = 1; $d4 <= 6; $d4++) {
for ($d5 = 1; $d5 <= 6; $d5++) {
for ($d6 = 1; $d6 <= 6; $d6++) {
@roll = sort ($d1, $d2, $d3, $d4, $d5, $d6);
&analyze(0);
}
}
}
}
}
}
| Dice Groupings | No. of Unique Ways to Roll |
|---|---|
| 6 | 6 |
| 5 + 1 | 180 |
| 4 + 2 | 450 |
| 4 + 1 + 1 | 1800 |
| 3 + 3 | 300 |
| 3 + 2+ 1 | 7200 |
| 2 + 2 + 2 | 1800 |
| 2 + 2 + 1 + 1 | 16200 |
| 3 + 1 + 1 + 1 | 7200 |
| 2 + 1 + 1 + 1 + 1 | 10800 |
| 1 + 1 + 1 + 1 + 1 + 1 | 720 |
So now, all I have to do to scratch that itch is figure out how to get these numbers, um, analytically. (Technically, I've already done so for grouping 6, 5+1, 4+2, and 1+1+1+1+1+1. The rest? I'm missing something. Worse, since I'm using the same approach to get both right and wrong answers, maybe I'm getting my right answers... by accident?)
But I think I have quieted my inner geek, for the moment. There are other things to do, and daylight is burning!