aces and dice

import sys
from itertools import permutations
from itertools import combinations_with_replacement

good = set()
bad =  set()
uniquerolls = set()


def looptest(o, i, j, k , l):
    # firstdice are the only possible solutions for the first two dice
    firstdice = ["43", "34", "52", "25", "61", "16", "51", "15", "62", "26", "53", "35", "44"]
    flag = 0
    # see if the first two die add to 5, 6, 7 or 8
    # if they do, test the other two dice
    case = o[i] + o[j]

    if case in firstdice:
        flag = check(o, int(o[k]), int(o[l]), int(o[i]) + int(o[j]))
    return(flag)



def check(o, i,j,pair):
    # these are the rules for determining if you can deal to the aces depending on the sum of the first two dice
    if pair == 6:
        # if the first two dice add to 6, then one of the other two must be six to work
        if i == 6 or j == 6:
            return(1)
    elif pair == 7:
        # if the first two dice add to 7 then then one of the other two must be a 5 or a 6 to work
        if i == 5 or i == 6 or j == 5 or j == 6:
            return (1)
    elif pair == 8:
        if (i + j == 6) or (i + j) == 7 or (i + j == 8):
            return (1)

    return(0)

def main():

# uniquerolls equals all possible combinations with replacemenst of four die (126)
    uniquerolls = list(p for p in  combinations_with_replacement("123456",4))

# there are permutations of the dice to test
    for i in uniquerolls:
        roll = [i[0],i[1],i[2],i[3]]  # convert the string into a list of charactes
        perms = (p for p in permutations("0123")) # set up all permutations of the four dice so we can look at each pair
        for j in perms:
            # check the permutations of this roll looking for a solution
            flag = looptest(roll, int(j[0]), int(j[1]), int(j[2]), int(j[3]))
            if (flag == 1):
                roll.sort() # sort the roll and add it to the good set.  Sorting makes sure we only add one roll
                good.add(str(roll[0]) + str(roll[1]) + str(roll[2]) + str(roll[3]))
                break  # found a good roll so don't check any more permutations
        if (flag == 0):
            roll.sort()
            bad.add(str(roll[0]) + str(roll[1]) + str(roll[2]) + str(roll[3]))


    print("bad", len(bad))
    print("good", len(good))
    print("unique rolls", len(list(uniquerolls)))
    print(sorted(good))
    print(sorted(bad))

    over, under = 0,0
    for i in bad:
        if int(i[0])+int(i[1])+int(i[2])+int(i[3]) > 13:
            over = over + 1
        else:
            under = under + 1

    print("over",over,"under",under)
main()