From 2aa6dc3045d691d2ab684d34687bca1449c5354a Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Sat, 16 Dec 2017 23:53:07 -0800 Subject: [PATCH] Day 6 --- day6.py | 39 +++++++++++++++++++++++++++++++++++++++ input/day6 | 1 + 2 files changed, 40 insertions(+) create mode 100644 day6.py create mode 100644 input/day6 diff --git a/day6.py b/day6.py new file mode 100644 index 0000000..f8d40dc --- /dev/null +++ b/day6.py @@ -0,0 +1,39 @@ +with open('input/day6') as f: + puzzle_input = tuple(map(int, f.read().split())) + +def cycle(banks): + banks = list(banks) + idx, blocks = max(enumerate(banks), key=lambda x: x[1]) + banks[idx] = 0 + idx = (idx+1) % len(banks) + while blocks: + banks[idx] += 1 + blocks -= 1 + idx = (idx+1) % len(banks) + return tuple(banks) + +def reallocate(banks): + seen = set() + seen.add(banks) + count = 0 + while True: + banks = cycle(banks) + count += 1 + if banks in seen: + return count + seen.add(banks) + +print(reallocate(puzzle_input)) + +def reallocate_loop(banks): + seen = {} + time = 0 + seen[banks] = time + while True: + banks = cycle(banks) + time += 1 + if banks in seen: + return time - seen[banks] + seen[banks] = time + +print(reallocate_loop(puzzle_input)) diff --git a/input/day6 b/input/day6 new file mode 100644 index 0000000..da97489 --- /dev/null +++ b/input/day6 @@ -0,0 +1 @@ +2 8 8 5 4 2 3 1 5 5 1 2 15 13 5 14 \ No newline at end of file