|
| 1 | +# --- Day 6: Memory Reallocation --- |
| 2 | +# |
| 3 | +# A debugger program here is having an issue: it is trying to repair a memory |
| 4 | +# reallocation routine, but it keeps getting stuck in an infinite loop. |
| 5 | +# |
| 6 | +# In this area, there are sixteen memory banks; each memory bank can hold any |
| 7 | +# number of blocks. The goal of the reallocation routine is to balance the |
| 8 | +# blocks between the memory banks. |
| 9 | +# |
| 10 | +# The reallocation routine operates in cycles. In each cycle, it finds the |
| 11 | +# memory bank with the most blocks (ties won by the lowest-numbered memory bank) |
| 12 | +# and redistributes those blocks among the banks. To do this, it removes all of |
| 13 | +# the blocks from the selected bank, then moves to the next (by index) memory |
| 14 | +# bank and inserts one of the blocks. It continues doing this until it runs |
| 15 | +# out of blocks; if it reaches the last memory bank, it wraps around to the |
| 16 | +# first one. |
| 17 | +# |
| 18 | +# The debugger would like to know how many redistributions can be done before a |
| 19 | +# blocks-in-banks configuration is produced that has been seen before. |
| 20 | +# |
| 21 | +# For example, imagine a scenario with only four memory banks: |
| 22 | +# |
| 23 | +# The banks start with 0, 2, 7, and 0 blocks. The third bank has the most |
| 24 | +# blocks, so it is chosen for redistribution. |
| 25 | +# Starting with the next bank (the fourth bank) and then continuing to the |
| 26 | +# first bank, the second bank, and so on, the 7 blocks are spread out over the |
| 27 | +# memory banks. The fourth, first, and second banks get two blocks each, |
| 28 | +# and the third bank gets one back. The final result looks like this: 2 4 1 2. |
| 29 | +# Next, the second bank is chosen because it contains the most blocks (four). |
| 30 | +# Because there are four memory banks, each gets one block. |
| 31 | +# The result is: 3 1 2 3. |
| 32 | +# |
| 33 | +# Now, there is a tie between the first and fourth memory banks, both of which |
| 34 | +# have three blocks. The first bank wins the tie, and its three blocks are |
| 35 | +# distributed evenly over the other three banks, leaving it with none: 0 2 3 4. |
| 36 | +# The fourth bank is chosen, and its four blocks are distributed such that |
| 37 | +# each of the four banks receives one: 1 3 4 1. |
| 38 | +# The third bank is chosen, and the same thing happens: 2 4 1 2. |
| 39 | +# At this point, we've reached a state we've seen before: 2 4 1 2 was already |
| 40 | +# seen. The infinite loop is detected after the fifth block redistribution |
| 41 | +# cycle, and so the answer in this example is 5. |
| 42 | +# |
| 43 | +# Given the initial block counts in your puzzle input, how many redistribution |
| 44 | +# cycles must be completed before a configuration is produced that has been |
| 45 | +# seen before? |
| 46 | +# |
| 47 | +# ------------------------------------------------------------------------------ |
| 48 | + |
| 49 | +import os |
| 50 | + |
| 51 | +class Debugger: |
| 52 | + |
| 53 | + def __init__(self, banks=[]): |
| 54 | + self.banks = banks |
| 55 | + self.bank_states = [] |
| 56 | + |
| 57 | + def solve(self): |
| 58 | + while self.banks not in self.bank_states: |
| 59 | + self.bank_states.append(self.banks) |
| 60 | + next_bank = self.next() |
| 61 | + self.banks = next_bank |
| 62 | + return len(self.bank_states) |
| 63 | + |
| 64 | + def solve2(self): |
| 65 | + return len(self.bank_states) - self.bank_states.index(self.banks) |
| 66 | + |
| 67 | + def next(self): |
| 68 | + banks = self.banks.copy() |
| 69 | + i = banks.index(max(banks)) |
| 70 | + val = banks[i] |
| 71 | + banks[i] = 0 |
| 72 | + while val > 0: |
| 73 | + i += 1 |
| 74 | + if i == len(banks): |
| 75 | + i = 0 |
| 76 | + banks[i] += 1 |
| 77 | + val -= 1 |
| 78 | + return banks |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "inputs/day06.txt") |
| 82 | + f = open(file_path) |
| 83 | + data = f.read().split('\t') |
| 84 | + debugger = Debugger([int(d) for d in data]) |
| 85 | + print(debugger.solve()) |
| 86 | + print(debugger.solve2()) |
| 87 | + |
0 commit comments