-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_code.py
More file actions
53 lines (42 loc) · 1.29 KB
/
Copy path06_code.py
File metadata and controls
53 lines (42 loc) · 1.29 KB
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
53
def memory_reallocation(blocks):
redistribution = 0
states = set()
seen_state = False
while not seen_state:
distribute_memory = max(blocks)
curr_index = blocks.index(distribute_memory)
blocks[curr_index] = 0
while distribute_memory > 0:
curr_index += 1
blocks[curr_index % len(blocks)] += 1
distribute_memory += -1
redistribution += 1
if tuple(blocks) not in states:
states.add(tuple(blocks))
else:
seen_state = True
print redistribution
def part_2(blocks):
redistribution = 0
states = []
seen_state = False
while not seen_state:
distribute_memory = max(blocks)
curr_index = blocks.index(distribute_memory)
blocks[curr_index] = 0
while distribute_memory > 0:
curr_index += 1
blocks[curr_index % len(blocks)] += 1
distribute_memory += -1
redistribution += 1
if tuple(blocks) not in states:
states.append(tuple(blocks))
else:
seen_state = True
print redistribution - states.index(tuple(blocks)) - 1
f = open('06_input.txt', 'r')
blocks = map(int, f.readline().split('\t'))
# Part 1
# memory_reallocation(blocks)
# Part 2
# part_2(blocks)