瀏覽代碼

Day 17.

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

+ 16
- 0
17.py 查看文件

@@ -0,0 +1,16 @@
1
+with open('data/17.txt', 'r') as file:
2
+    steps = int(file.readline().strip())
3
+    buffer = []
4
+    position = 0
5
+    for i in range(2018):
6
+        buffer.insert(position, i)
7
+        position = (position + steps + 1) % (i + 1)
8
+    print(f'Part one: {buffer[(buffer.index(2017) + 1) % len(buffer)]}')
9
+
10
+    # The entry for zero is always at the end of our buffer, as we never call insert beyond the length of the list,
11
+    # so the entry after zero is always the one at position 0.
12
+    for i in range(2018, 50000001):
13
+        if position == 0:
14
+            after_zero = i
15
+        position = (position + steps + 1) % (i + 1)
16
+    print(f'Part two: {after_zero}')

+ 1
- 0
data/17.txt 查看文件

@@ -0,0 +1 @@
1
+355

Loading…
取消
儲存