How to resolve the algorithm Dice game probabilities step by step in the Raku programming language
How to resolve the algorithm Dice game probabilities step by step in the Raku programming language
Table of Contents
Problem Statement
Two players have a set of dice each. The first player has nine dice with four faces each, with numbers one to four. The second player has six normal dice with six faces each, each face has the usual numbers from one to six. They roll their dice and sum the totals of the faces. The player with the highest total wins (it's a draw if the totals are the same). What's the probability of the first player beating the second player? Later the two players use a different set of dice each. Now the first player has five dice with ten faces each, and the second player has six dice with seven faces each. Now what's the probability of the first player beating the second player? This task was adapted from the Project Euler Problem n.205: https://projecteuler.net/problem=205
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Dice game probabilities step by step in the Raku programming language
Source code in the raku programming language
sub likelihoods ($roll) {
my ($dice, $faces) = $roll.comb(/\d+/);
my @counts;
@counts[$_]++ for [X+] |(1..$faces,) xx $dice;
return [@counts[]:p], $faces ** $dice;
}
sub beating-probability ([$roll1, $roll2]) {
my (@c1, $p1) := likelihoods $roll1;
my (@c2, $p2) := likelihoods $roll2;
my $p12 = $p1 * $p2;
[+] gather for flat @c1 X @c2 -> $p, $q {
take $p.value * $q.value / $p12 if $p.key > $q.key;
}
}
# We're using standard DnD notation for dice rolls here.
say .gist, "\t", .raku given beating-probability < 9d4 6d6 >;
say .gist, "\t", .raku given beating-probability < 5d10 6d7 >;
You may also check:How to resolve the algorithm Long multiplication step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the Ring programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the zkl programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Oforth programming language