Shell Game

January 2019 Bronze - Problem 1: shell

Algorithm

See how many correct guesses Elsie makes if the pebble starts under shell A, then shell B, then shell C.

Python

def run(swaps):
    current = [1,2,3] # The current position of the shells
    correct = [0,0,0] # If pebble starts at shell i
    for swap in swaps:
        current[swap[1]-1], current[swap[0]-1] = current[swap[0]-1], current[swap[1]-1]
        correct[current[swap[2]-1]-1]+=1
    print(correct)
    return max(correct)

with open('shell.in','r') as fIn:
    n = int(fIn.readline())
    swaps = []
    for i in range(n):
        swaps.append(list(map(int,fIn.readline().split())))
with open('shell.out','w') as fOut:
    fOut.write(str(run(swaps))+'\n')