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.




 
import algorithm, sequtils, sets, strformat
import itertools
 
type Die = object
  name: string
  faces: seq[int]
 
 
####################################################################################################
# Die functions.
 
 
func `$`(die: Die): string =
  ## Return the string representation of a Die.
  &"({die.name}: {($die.faces)[1..^1]})"
 
 
func cmp(die1, die2: Die): int =
  ## Compare two die returning 1, -1 or 0 for operators >, < ==.
  var tot: array[3, int]
  for d in product(die1.faces, die2.faces):
    inc tot[1 + ord(d[1] < d[0]) - ord(d[0] < d[1])]
  result = ord(tot[0] < tot[2]) - ord(tot[2] < tot[0])
 
 
func verboseCmp(die1, die2: Die): string =
  ## Compare tow die returning a string.
  var win1, win2 = 0
  for (d1, d2) in product(die1.faces, die2.faces):
    inc win1, ord(d1 > d2)
    inc win2, ord(d2 > d1)
  result = if win1 > win2: &"{die1.name} > {die2.name}"
           elif win1 < win2: &"{die1.name} < {die2.name}"
           else: &"{die1.name} = {die2.name}"
 
 
func `>`(die1, die2: Die): bool = cmp(die1, die2) > 0
func `<=`(die1, die2: Die): bool = cmp(die1, die2) <= 0
 
 
####################################################################################################
# Added a permutation iterator as that of "itertools" doesn't allow to specify the length.
 
 
iterator permutations[T](values: openArray[T]; r: int): seq[T] {.closure} =
  ## Yield permutations of length "r" with elements taken from "values".
  let n = values.len
  if r > n: return
  var indices = toSeq(0..<n)
  var cycles = toSeq(countdown(n, n - r + 1))
  var perm = values[0..<r]
  yield perm
  if n == 0: return
  while true:
    var exit = true
    for i in countdown(r - 1, 0):
      dec cycles[i]
      if cycles[i] == 0:
        discard indices.rotateLeft(i..indices.high, 1)
        cycles[i] = n - i
      else:
        let j = cycles[i]
        swap indices[i], indices[^j]
        for iperm, ivalues in indices[0..<r]:
          perm[iperm] = values[ivalues]
        yield perm
        exit = false
        break
    if exit: return
 
 
####################################################################################################
# Dice functions.
 
 
func isNonTrans(dice: openArray[Die]): bool =
  ## Return true if ordering of die in dice is non-transitive.
  for i in 1..dice.high:
    if dice[i] <= dice[i-1]: return false
  result = dice[0] > dice[^1]
 
 
func findNonTrans(allDice: openArray[Die]; n = 3): seq[seq[Die]] =
  ## Return the list of non-transitive dice.
  for perm in permutations(allDice, n):
    if perm.isNontrans:
      result.add perm
 
 
proc possibleDice(sides, maxval: Positive): seq[Die] =
  ## Return the list of possible dice with given number of sides and maximum value.
 
  echo &"All possible 1..{maxval} {sides}-sided dice."
  var dice: seq[Die]
  var n = 1
  for faces in product(toSeq(1..maxval), repeat = sides):
    dice.add Die(name: &"D{n}", faces: faces)
    inc n
  echo &"  Created {dice.len} dice."
  echo "  Remove duplicate with same bag of numbers on different faces."
  var found: HashSet[seq[int]]
  for d in dice:
    let count = sorted(d.faces)
    if count notin found:
      found.incl count
      result.add d
  echo &"   Return {result.len} filtered dice."
 
 
func verboseDiceCmp(dice: openArray[Die]): string =
  ## Return the verbose comparison of dice.
  for i in 1..dice.high:
    result.add verboseCmp(dice[i-1], dice[i]) & ", "
  result.add verboseCmp(dice[0], dice[^1])
 
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
when isMainModule:
 
  let dice = possibleDice(sides = 4, maxval = 4)
  for n in [3, 4]:
    let nonTrans = dice.findNonTrans(n)
    echo &"\n  Non-transitive length-{n} combinations found: {nonTrans.len}."
    for list in nonTrans:
      echo ""
      for i, die in list:
        echo "    ", if i == 0: '[' else: ' ', die, if i == list.high: "]" else: ","
    if nonTrans.len != 0:
      echo &"\n  More verbose comparison of last non-transitive result:"
      echo "  ", verboseDiceCmp(nonTrans[^1])
    echo "\n  ===="