Let our dice select numbers on their faces with equal probability, i.e. fair dice.
Dice may have more or less than six faces. (The possibility of there being a
3D physical shape that has that many "faces" that allow them to be fair dice,
is ignored for this task - a die with 3 or 33 defined sides is defined by the
number of faces and the numbers on each face).

Throwing dice will randomly select a face on each die with equal probability.
To show which die of dice thrown multiple times is more likely to win over the
others:

 calculate all possible combinations of different faces from each die
 Count how many times each die wins a combination
 Each combination is equally likely so the die with more winning face combinations is statistically more likely to win against the other dice.


If two dice X and Y are thrown against each other then X likely to: win, lose, or break-even against Y can be shown as: X > Y, X < Y, or X = Y respectively.

Example 1

If X is the three sided die with 1, 3, 6 on its faces and Y has 2, 3, 4 on its
faces then the equal possibility outcomes from throwing both, and the winners
is:

   X   Y   Winner
   =   =   ======
   1   2   Y
   1   3   Y
   1   4   Y
   3   2   X
   3   3   -
   3   4   Y
   6   2   X
   6   3   X
   6   4   X
   
   TOTAL WINS: X=4, Y=4

Both die will have the same statistical probability of winning, i.e.their comparison can be written as X = Y

Transitivity

In mathematics transitivity are rules like:

   if a op b and b op c then a op c

If, for example, the op, (for operator), is the familiar less than, <, and it's applied to integers
we get the familiar if a < b and b < c then a < c

Non-transitive dice
These are an ordered list of dice where the '>' operation between successive
dice pairs applies but a comparison between the first and last of the list
yields the opposite result, '<'.

(Similarly '<' successive list comparisons with a final '>' between first and last is also non-transitive).

Three dice S, T, U with appropriate face values could satisfy

   S < T, T < U and yet S > U

To be non-transitive.

Notes

 The order of numbers on the faces of a die is not relevant. For example, three faced die described with face numbers of 1, 2, 3 or 2, 1, 3 or any other permutation are equivalent. For the purposes of the task show only the permutation in lowest-first sorted order i.e. 1, 2, 3 (and remove any of its perms).
 A die can have more than one instance of the same number on its faces, e.g. 2, 3, 3, 4
 Rotations: Any rotation of non-transitive dice from an answer is also an answer. You may optionally compute and show only one of each such rotation sets, ideally the first when sorted in a natural way. If this option is used then prominently state in the output that rotations of results are also solutions.


Task

====
Find all the ordered lists of three non-transitive dice S, T, U of the form
S < T, T < U and yet S > U; where the dice are selected from all four-faced die
, (unique w.r.t the notes), possible by having selections from the integers
one to four on any dies face.

Solution can be found by generating all possble individual die then testing all
possible permutations, (permutations are ordered), of three dice for
non-transitivity.

Optional stretch goal

Find lists of four non-transitive dice selected from the same possible dice from the non-stretch goal.




Show the results here, on this page.



References

 The Most Powerful Dice - Numberphile Video.
 Nontransitive dice - Wikipedia.




use strict;
use warnings;
 
sub fourFaceCombs {
    my %found = ();
    my @res = ();
    for (my $i = 1; $i <= 4; $i++) {
        for (my $j = 1; $j <= 4; $j++) {
            for (my $k = 1; $k <= 4; $k++) {
                for (my $l = 1; $l <= 4; $l++) {
                    my @c = sort ($i, $j, $k, $l);
                    my $key = 0;
                    for my $p (@c) {
                        $key = 10 * $key + $p;
                    }
                    if (not exists $found{$key}) {
                        $found{$key} = 1;
                        push @res, \@c;
                    }
                }
            }
        }
    }
    return @res;
}
 
sub compare {
    my $xref = shift;
    my $yref = shift;
 
    my @x = @$xref;
    my $xw = 0;
 
    my @y = @$yref;
    my $yw = 0;
 
    for my $i (@x) {
        for my $j (@y) {
            if ($i < $j) {
                $yw++;
            }
            if ($j < $i) {
                $xw++;
            }
        }
    }
 
    if ($xw < $yw) {
        return -1;
    }
    if ($yw < $xw) {
        return 1;
    }
    return 0;
}
 
sub findIntransitive3 {
    my $dice_ref = shift;
    my @dice = @$dice_ref;
    my $len = scalar @dice;
 
    my @res = ();
    for (my $i = 0; $i < $len; $i++) {
        for (my $j = 0; $j < $len; $j++) {
            my $first = compare($dice[$i], $dice[$j]);
            if ($first == 1) {
                for (my $k = 0; $k < $len; $k++) {
                    my $second = compare($dice[$j], $dice[$k]);
                    if ($second == 1) {
                        my $third = compare($dice[$k], $dice[$i]);
                        if ($third == 1) {
                            my $d1r = $dice[$i];
                            my $d2r = $dice[$j];
                            my $d3r = $dice[$k];
                            my @itd = ($d1r, $d2r, $d3r);
                            push @res, \@itd;
                        }
                    }
                }
            }
        }
    }
    return @res;
}
 
sub findIntransitive4 {
    my $dice_ref = shift;
    my @dice = @$dice_ref;
    my $len = scalar @dice;
 
    my @res = ();
    for (my $i = 0; $i < $len; $i++) {
        for (my $j = 0; $j < $len; $j++) {
            for (my $k = 0; $k < $len; $k++) {
                for (my $l = 0; $l < $len; $l++) {
                    my $first = compare($dice[$i], $dice[$j]);
                    if ($first == 1) {
                        my $second = compare($dice[$j], $dice[$k]);
                        if ($second == 1) {
                            my $third = compare($dice[$k], $dice[$l]);
                            if ($third == 1) {
                                my $fourth = compare($dice[$l], $dice[$i]);
                                if ($fourth == 1) {
                                    my $d1r = $dice[$i];
                                    my $d2r = $dice[$j];
                                    my $d3r = $dice[$k];
                                    my $d4r = $dice[$l];
                                    my @itd = ($d1r, $d2r, $d3r, $d4r);
                                    push @res, \@itd;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return @res;
}
 
sub main {
    my @dice = fourFaceCombs();
    my $len = scalar @dice;
    print "Number of eligible 4-faced dice: $len\n\n";
 
    my @it3 = findIntransitive3(\@dice);
    my $count3 = scalar @it3;
    print "$count3 ordered lists of 3 non-transitive dice found, namely:\n";
    for my $itref (@it3) {
        print "[ ";
        for my $r (@$itref) {
            print "[@$r] ";
        }
        print "]\n";
    }
    print "\n";
 
    my @it4 = findIntransitive4(\@dice);
    my $count = scalar @it4;
    print "$count ordered lists of 4 non-transitive dice found, namely:\n";
    for my $itref (@it4) {
        print "[ ";
        for my $r (@$itref) {
            print "[@$r] ";
        }
        print "]\n";
    }
}
 
main();