-
Notifications
You must be signed in to change notification settings - Fork 0
/
d6.py
62 lines (50 loc) · 1.46 KB
/
d6.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
53
54
55
56
57
58
59
60
61
# Advent of code - day 6
data = [11, 11, 13, 7, 0, 15, 5, 5, 4, 4, 1, 1, 7, 1, 15, 11]
# data = [0, 2, 7, 0]
# find memory bank with most blocks - returns [position, value]
def find_memorybank(data_memory):
pos = 0
blocks = 0
counter = 0
for i in data_memory:
if i > blocks:
blocks = i
pos = counter
counter += 1
return pos, blocks
def redistribute(data_mem):
pos, blocks = find_memorybank(data_mem)
data_mem[pos] = 0
memorybanks = len(data_mem) - 1
current_pos = pos
for i in range(blocks):
if current_pos + 1 > memorybanks:
current_pos = 0
else:
current_pos += 1
data_mem[current_pos] += 1
return data_mem
# start memory redistribution and break when loop is found!
positions = list()
positions.append(data.copy())
no_duplicates = True
redistributions = 0
while no_duplicates:
new_position = redistribute(data).copy()
redistributions += 1
for pos in positions:
if pos == new_position:
no_duplicates = False
break
positions.append(new_position)
# get the size of the loop
loop_content = positions[-1].copy()
counter = 0
for p in positions:
if p == loop_content:
break
counter += 1
loop_size = len(positions) - 1 - counter
# print(str(positions))
print(' Part one - Number of redistributions when duplicate found: ' + str(redistributions))
print(' Part two - Loop size: ' + str(loop_size))