Solution to problem 6 in Python
This commit is contained in:
parent
3e35e6caf9
commit
8bbdba9dfd
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user