From 8bbdba9dfd54fbf39eb732e1793b714b61a2c8be Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sat, 5 Mar 2022 15:25:10 +0100 Subject: [PATCH] Solution to problem 6 in Python --- src/Year_2017/P6.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/Year_2017/P6.py b/src/Year_2017/P6.py index 7c6b022..cde7765 100644 --- a/src/Year_2017/P6.py +++ b/src/Year_2017/P6.py @@ -96,5 +96,44 @@ def part_1() -> None: print(f"{iteration} cycles must be completed") +# --- Part Two --- + +# Out of curiosity, the debugger would also like to know the size of the loop: +# starting from a state that has already been seen, how many block +# redistribution cycles must be performed before that same state is seen again? + +# In the example above, 2 4 1 2 is seen again after four cycles, and so the +# answer in that example would be 4. + +# How many cycles are in the infinite loop that arises from the configuration +# in your puzzle input? + + +def part_2() -> None: + seen = [] + repeated = False + iteration = 0 + while not repeated: + iteration += 1 + counts = Counter(banks).most_common() + # get the most common (tuple) + _most_common = counts[0] + # reset its key + banks[_most_common[0]] = 0 + for key in cycle_through( + banks, int(_most_common[0]) + 1, int(_most_common[1]) + ): + banks[key] += 1 + _iter = Counter(banks).most_common() + if _iter in seen: + repeated = True + else: + if iteration > 1: + seen.append(_iter) + + print(f"There are {len(seen)} cycles in the infinite loop") + + if __name__ == "__main__": part_1() + part_2()