Problem solution · Python

CCC 2013 S3 - Chances of Winning

CCC 2013 S3 - Chances of Winning: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
159 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 2013 S3 - Chances of Winning, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 159 lines of Python from the credited upstream file ccc13s3.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2013 S3 - Chances of Winning · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2013 Senior 3: Chances of Winning## there are 6 games: 1-2, 1-3, 1-4, 2-3, 2-4, 3-4# (or using 0 thru 3: 0-1, 0-2, 0-3, 1-2, 1-3, 2-3)# each game is either a "W", "L", "T" wrt the 1st team.# thus each combo of games could be represented as something like "WLLTWT" ## With no games played, set the original string to "------",# or after a few games, set it to something like "-W--L-"## Using that original string create a list of all other possible games.## Count those strings that win for T.  # given a string (s) such as "WLTTLW" and a t determine if# s results in a winning combo for t (0-3).def winning(s, t):    score = [0,0,0,0]    if s[0] == "W":        score[0] = score[0] + 3        score[1] = score[1] + 0    elif s[0] == "L":        score[0] = score[0] + 0        score[1] = score[1] + 3    else:        score[0] = score[0] + 1        score[1] = score[1] + 1    if s[1] == "W":        score[0] = score[0] + 3        score[2] = score[2] + 0    elif s[1] == "L":        score[0] = score[0] + 0        score[2] = score[2] + 3    else:        score[0] = score[0] + 1        score[2] = score[2] + 1    if s[2] == "W":        score[0] = score[0] + 3        score[3] = score[3] + 0    elif s[2] == "L":        score[0] = score[0] + 0        score[3] = score[3] + 3    else:        score[0] = score[0] + 1        score[3] = score[3] + 1    if s[3] == "W":        score[1] = score[1] + 3        score[2] = score[2] + 0    elif s[3] == "L":        score[1] = score[1] + 0        score[2] = score[2] + 3    else:        score[1] = score[1] + 1        score[2] = score[2] + 1    if s[4] == "W":        score[1] = score[1] + 3        score[3] = score[3] + 0    elif s[4] == "L":        score[1] = score[1] + 0        score[3] = score[3] + 3    else:        score[1] = score[1] + 1        score[3] = score[3] + 1    if s[5] == "W":        score[2] = score[2] + 3        score[3] = score[3] + 0    elif s[5] == "L":        score[2] = score[2] + 0        score[3] = score[3] + 3    else:        score[2] = score[2] + 1        score[3] = score[3] + 1    if score[t] == max(score) and score.count(max(score)) == 1:        return True    else:        return False file = open("s3.8.in", 'r')t = int(file.readline()) - 1g = int(file.readline()) original = "------"for i in range(g):    x = file.readline().strip().split()    a = int(x[0]) - 1    b = int(x[1]) - 1    sa = x[2]    sb = x[3]    if sa > sb:        letter = "W"     elif sa < sb:        letter = "L"     else:        letter = "T"     if a == 0 and b == 1:        original = letter + original[1:]    elif a == 0 and b == 2:        original = original[0] + letter + original[2:]    elif a == 0 and b == 3:        original = original[:2] + letter + original[3:]    elif a == 1 and b == 2:        original = original[:3] + letter + original[4:]    elif a == 1 and b == 3:        original = original[:4] + letter + original[5]    elif a == 2 and b == 3:        original = original[:5] + letter  choice = "WLT"possible = [] goto = []for i in range(6):    if original[i] == "-":        goto.append(3)    else:        goto.append(1) for a in range(goto[0]):    for b in range(goto[1]):        for c in range(goto[2]):            for d in range(goto[3]):                for e in range(goto[4]):                    for f in range(goto[5]):                        s = ""                        if goto[0] == 1:                            s = s + original[0]                        else:                            s = s + choice[a]                        if goto[1] == 1:                            s = s + original[1]                        else:                            s = s + choice[b]                        if goto[2] == 1:                            s = s + original[2]                        else:                            s = s + choice[c]                        if goto[3] == 1:                            s = s + original[3]                        else:                            s = s + choice[d]                        if goto[4] == 1:                            s = s + original[4]                        else:                            s = s + choice[e]                        if goto[5] == 1:                            s = s + original[5]                        else:                            s = s + choice[f]                        possible.append(s) count = 0for s in possible:    if winning(s, t):        count = count + 1 print count  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗