memorybanks = [14, 0, 15, 12, 11, 11, 3, 5, 1, 6, 8, 4, 9, 1, 8, 4] def findSeenBefore(banks): blocks = tuple(banks) seenBefore = {} steps = 0 while (blocks not in seenBefore): seenBefore[blocks] = True maxValue = max(blocks) indexOfMax = blocks.index(maxValue) numberOfElements = len(blocks) blocksToRedistribute = (maxValue // (numberOfElements - 1)) or 1 banks[indexOfMax] = 0 for i in (list(range(indexOfMax + 1, numberOfElements)) + list(range(0, indexOfMax + 1))): blocksToMove = maxValue if maxValue < blocksToRedistribute else blocksToRedistribute banks[i] += blocksToMove maxValue -= blocksToMove steps += 1 blocks = tuple(banks) return blocks, steps blocks, steps = findSeenBefore(memorybanks) print("Part1 Last State") print("-----------------------") print(blocks) print("Cycles") print("-----------------------") print(steps) blocks, steps = findSeenBefore(list(blocks)) print("Part2 Last State") print("-----------------------") print(blocks) print("Cycles") print("-----------------------") print(steps)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question