|
@@ -0,0 +1,24 @@
|
|
1
|
+import operator
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+def redistribute(buckets):
|
|
5
|
+ while True:
|
|
6
|
+ max_index, max_value = max(enumerate(buckets), key=operator.itemgetter(1))
|
|
7
|
+ buckets[max_index] = 0
|
|
8
|
+ for i in range(max_value):
|
|
9
|
+ buckets[(max_index + i + 1) % len(buckets)] += 1
|
|
10
|
+ yield buckets
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+def find_loop(buckets):
|
|
14
|
+ history = [list(buckets)]
|
|
15
|
+ for i, new_bucket in enumerate(redistribute(buckets), start=1):
|
|
16
|
+ if new_bucket in history:
|
|
17
|
+ return i
|
|
18
|
+ history.append(list(buckets))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+with open('data/06.txt', 'r') as file:
|
|
22
|
+ buckets = list(map(int, file.readline().split('\t')))
|
|
23
|
+ print(f'Part one: {find_loop(buckets)}')
|
|
24
|
+ print(f'Part two: {find_loop(buckets)}')
|