Browse Source

Day 6.

master
Chris Smith 6 years ago
parent
commit
17e847bdaf
2 changed files with 25 additions and 0 deletions
  1. 24
    0
      06.py
  2. 1
    0
      data/06.txt

+ 24
- 0
06.py View File

@@ -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)}')

+ 1
- 0
data/06.txt View File

@@ -0,0 +1 @@
1
+4	1	15	12	0	9	9	5	5	8	7	3	14	5	12	3

Loading…
Cancel
Save