Jan. 11th, 2014

alexpgp: (Default)
It turns out that some of the Farkel-related numbers I've been seeing take account of the rules of the game, which apparently confer special meaning to dice showing one or five. So I'm not at all sure such numbers are of much help.

And then it struck me: brute-forcing the six dice problem should not be all that hard.

Step 1. Log into my Raspberry Pi web server, open emacs and write a perl program to produce every possible roll of 6 dice.
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++) {
            my @roll = sort ($d1, $d2, $d3, $d4, $d5, $d6);
            print(@roll, "\n");
          }
        }
      }
    }
  }
}
Yes, I know: far from elegant. But it gets the job done. I run this program from the command line and I direct its output to allrolls.txt.

Step 2. Run the Linux command line
sort allrolls.txt | uniq -c > uniquerolls.txt
What this command line does is sort the allrolls.txt file and creates an output file (uniquerolls.txt)where duplicate lines are replaced with a single line that's prefaced by the number of duplicates found in the input file, e.g.:
      1 111111
      6 111112
      6 111113
      6 111114
      6 111115
      6 111116
     15 111122
     30 111123
     30 111124
     30 111125
     30 111126
     ...
If I count the number of lines in the file uniquerolls.txt, it turns out there are only 462 different combinations of 1, 2, 3, 4, 5 and 6 possible with 6 dice, though some (112456, which can occur 360 different ways) are more probable than others (111111, which can occur in exactly one way).

Progress, surely, but Morpheus calls.
alexpgp: (Visa)
My 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:
my @result = ();
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);
          }
        }
      }
    }
  }
}
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):

Dice GroupingsNo. of Unique Ways to Roll
66
5 + 1180
4 + 2450
4 + 1 + 11800
3 + 3300
3 + 2+ 17200
2 + 2 + 21800
2 + 2 + 1 + 116200
3 + 1 + 1 + 17200
2 + 1 + 1 + 1 + 110800
1 + 1 + 1 + 1 + 1 + 1720

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!

Profile

alexpgp: (Default)
alexpgp

January 2018

S M T W T F S
  1 2 3456
7 8910111213
14 15 16 17181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 10th, 2025 08:48 am
Powered by Dreamwidth Studios