#!/usr/bin/env python def toStrList(my_list): return ','.join([str(item) for item in my_list]) myinput = [int(item) for item in "4 10 4 1 8 4 9 14 5 1 14 15 0 15 3 5".split()] myinputkey = toStrList(myinput) length = len(myinput) myinputfreq = {} steps = 0 max_i, max_ele = (0,0) while myinputkey not in myinputfreq or myinputfreq[myinputkey][0]<2: max_i, max_ele = max(enumerate(myinput), key=lambda p: p[1]) myinput[max_i] = 0 i = 1 while max_ele>0: myinput[(max_i+i)%length]+=1 max_ele-=1 i+=1 myinputkey = toStrList(myinput) if myinputkey not in myinputfreq: myinputfreq[myinputkey] = [1, steps] else: myinputfreq[myinputkey][0] += 1 myinputfreq[myinputkey][1] = steps - myinputfreq[myinputkey][1] steps+=1 print("Part 1: " + str(steps)) print("Part 2: " + str(myinputfreq[myinputkey][1]))