-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday06_reallocation.py
52 lines (37 loc) · 1.38 KB
/
day06_reallocation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Advent of Code 2017 -- Day 6
http://adventofcode.com/2017/day/6
"""
from typing import Tuple
PROBLEM_INPUT = "14 0 15 12 11 11 3 5 1 6 8 4 9 1 8 4"
TEST_INPUT_PROBLEM1 = "0 2 7 0"
def reallocate_current_block_state(curr_blocks: tuple) -> tuple:
blocks = list(curr_blocks)
num_blocks = len(blocks)
points_to_redistribute = max(blocks)
max_index = blocks.index(points_to_redistribute)
blocks[max_index] = 0
# redistribute its blocks amongst all other blocks
while points_to_redistribute > 0:
current_index = (max_index + points_to_redistribute) % num_blocks
blocks[current_index] += 1
points_to_redistribute -= 1
return tuple(blocks)
def memory_reallocation(memory: str) -> Tuple[str, str]:
blocks = tuple(int(block) for block in memory.split())
total_reallocs = 0
seen_before = {}
seen_before[blocks] = 0
while True:
# reallocate_blocks
blocks = reallocate_current_block_state(blocks)
total_reallocs += 1
if blocks in seen_before:
break
else:
seen_before[blocks] = total_reallocs
return (f'Total Realloc: {total_reallocs}',
f'Redistribution Cycles: {total_reallocs - seen_before[blocks]}')
if __name__ == '__main__':
print(memory_reallocation(TEST_INPUT_PROBLEM1))
print(memory_reallocation(PROBLEM_INPUT))
print('fin.')