瀏覽代碼

Day 6.

master
Chris Smith 6 年之前
父節點
當前提交
17e847bdaf
共有 2 個檔案被更改,包括 25 行新增0 行删除
  1. 24
    0
      06.py
  2. 1
    0
      data/06.txt

+ 24
- 0
06.py 查看文件

@@ -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 查看文件

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

Loading…
取消
儲存