#!/usr/bin/python3

import os, sys
from operator import itemgetter

class Mode:
    HTML = 0
    TEXT = 1
    R = 2

def main(mode):
    data = {}
    for points in range(0,10):
        # for ties, (n,m) means n of the m tied teams advance
        data[points] = {'total': 0, 'W': 0, 'L': 0, 'T': {(1,2): 0, (1,3): 0, (2,2): 0, (2,3): 0, (2,4): 0}}
    for result in getResults():
        outcome = getOutcome(result)
        usPoints = outcome[0]
        data[usPoints]['total'] += 1
        if outcome[1] == 'T':
            data[usPoints]['T'][outcome[2]] += 1
        else:
            data[usPoints][outcome[1]] += 1

    if mode == Mode.HTML:
        print("<table>")
        print("<tr><th># points</th><th>% to advance</th><th>% advance without tiebreaker</th></tr>")
    for points in range(0,10):
        d = data[points]
        if d['total'] > 0:
            tieProb = 0.0
            winNoTieProb = 0.0
            for key in d['T']:
                thisTieProb = (float(key[0])/key[1])*d['T'][key]
                if key[0] == 2 and key[1] == 2:
                    # we're guaranteed to advance
                    winNoTieProb += thisTieProb
                else:
                    tieProb += thisTieProb
            tieProb = tieProb / d['total']
            winNoTieProb = float(winNoTieProb + d['W']) / d['total']
            if mode == Mode.HTML:
                print("<tr><td>%d</td><td>%.2f%%</td><td>%.2f%%</td></tr>" % (points, (winNoTieProb+tieProb)*100, winNoTieProb*100))
            elif mode == Mode.TEXT:
                print('%d points: %.2f%% advance, %.2f%% advance w/o tie, %.2f%% tie out of %d outcomes' % (points, (winNoTieProb+tieProb)*100, winNoTieProb*100, tieProb*100, d['total']))
                #print d
            elif mode == Mode.R:
                print('%d\t%.2f\t%.2f' % (points, (winNoTieProb+tieProb)*100, winNoTieProb*100))
        elif mode == Mode.HTML:
            print("<tr><td>%d</td><td>-</td><td>-</td></tr>" % points)

    if mode == Mode.HTML:
        print("</table>")
    

def getOutcome(result):
    points = [0, 0, 0, 0]
    def accumPoints(a, b, result):
        if result == 0:
            points[a] += 3
        elif result == 1:
            points[b] += 3
        else:
            points[a] += 1
            points[b] += 1
    accumPoints(0, 1, result[0])
    accumPoints(0, 2, result[1])
    accumPoints(0, 3, result[2])
    accumPoints(1, 2, result[3])
    accumPoints(1, 3, result[4])
    accumPoints(2, 3, result[5])
    return getOutcomeFromPoints(points)

def getOutcomeFromPoints(points):
    keyedPoints = list(zip(list(range(4)), points))
    usPoints = points[0]
    sortedPoints = sorted(keyedPoints, key=itemgetter(1), reverse=True)
    minPointsToAdvance = sortedPoints[1][1]
    if usPoints > minPointsToAdvance:
        return [usPoints, 'W']
    elif usPoints < minPointsToAdvance:
        return [usPoints, 'L']
    # see how many are tied
    numTied = len([x for x in points if x == minPointsToAdvance])
    maxPoints = sortedPoints[0][1]

    if maxPoints == minPointsToAdvance:
        numThatGoOn = 2
    else:
        numThatGoOn = 1
    if numTied == numThatGoOn and numTied == 1:
        return [usPoints, 'W']
    else:
        return [usPoints, 'T', (numThatGoOn, numTied)]

# 0 = Win
# 1 = Loss
# 2 = Tie
# [AvsB, AvsC, AvsD, BvsC, BvsD, CvsD]
def getResults():
    result = [0, 0, 0, 0, 0, 0]
    l = len(result)
    while True:
        yield result
        i = 0
        while i < l:
            if result[l-i-1] < 2:
                result[l-i-1] += 1
                break
            else:
                result[l-i-1] = 0
                i += 1
        if i == l:
            break


if (__name__ == '__main__'):
    mode = Mode.TEXT
    if len(sys.argv) > 1:
        if sys.argv[1] == '-h':
            mode = Mode.HTML
        elif sys.argv[1] == '-r':
            mode = Mode.R
    main(mode)
