Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2017 Day 17 #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 2017/Day 17 - 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import collections


with open('Day 17 - input', 'r') as f:
data = f.readlines()

steps = int(data[0])
circular_buffer = collections.deque([0])
insertion_count = 2017

for i in range(1, insertion_count + 1):
# rotate buffer steps modulo current length
circular_buffer.rotate(-steps % i)
# append new value
circular_buffer.append(i)

# solution is first value in buffer
print(f"The value after {insertion_count} is {circular_buffer[0]}")
24 changes: 24 additions & 0 deletions 2017/Day 17 - 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import collections


with open('Day 17 - input', 'r') as f:
data = f.readlines()

steps = int(data[0])

circular_buffer = collections.deque([0])
insertion_count = 5000000
offset = 0

for i in range(1, insertion_count + 1):
# rotate buffer steps modulo current length
spin = -steps % i
circular_buffer.rotate(spin)
# append new value
circular_buffer.append(i)
# keep track of cummulative rotation
offset = (offset + spin) % i

# roll back to get value after 0
circular_buffer.rotate(-offset - 1)
print(f"The value after 0 the moment {insertion_count} is inserted: {circular_buffer[0]}")