-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06.py
More file actions
52 lines (39 loc) · 1.34 KB
/
day06.py
File metadata and controls
52 lines (39 loc) · 1.34 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
def calc_a(input):
states = set()
iterations = 0
bank = get_bank(input)
bank_size = len(bank)
while tuple(bank) not in states:
states.add(tuple(bank))
largest = max(bank)
index = bank.index(largest)
bank[index] = 0
to_distribute = largest
chunk_size = round(largest / bank_size, 0)
while to_distribute > 0:
index = (index + 1) % bank_size
bank[index] += chunk_size if chunk_size <= to_distribute else to_distribute
to_distribute -= chunk_size
iterations += 1
return iterations
def calc_b(input):
states = {}
iterations = 0
bank = get_bank(input)
bank_size = len(bank)
while tuple(bank) not in states:
states[tuple(bank)] = iterations
largest = max(bank)
index = bank.index(largest)
bank[index] = 0
to_distribute = largest
chunk_size = round(largest / bank_size, 0)
while to_distribute > 0:
index = (index + 1) % bank_size
bank[index] += chunk_size if chunk_size <= to_distribute else to_distribute
to_distribute -= chunk_size
iterations += 1
first_state_iteration = states[tuple(bank)]
return iterations - first_state_iteration
def get_bank(input):
return list(map(lambda i: int(i, 10), input.split()))