-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Hessner
committed
Dec 17, 2017
1 parent
ca80b46
commit 70f8620
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/python3 | ||
|
||
# http://adventofcode.com/2017/day/17 | ||
|
||
steps = 343 | ||
|
||
cbuf = [0] | ||
curpos = 0 | ||
|
||
for i in range(1,2017+1): | ||
curpos = (curpos+steps) % len(cbuf) + 1 # will always be >= 1 (important for part 2) | ||
cbuf[curpos:curpos] = [i] | ||
|
||
print("part 1", cbuf[cbuf.index(2017)+1]) | ||
|
||
|
||
# Since every value is inserted AFTER the current position and 0 is the first element, | ||
# we know that there will never be a value inserted before the 0. Thus, we do not need to | ||
# apply the inserts to the buffer, we just watch for every iterations where a value at position 1 (after the 0) | ||
# should be inserted. The last value is the value we are looking for | ||
# Not inserting elements to the buffer speeds up the algorithm | ||
curpos = 0 | ||
|
||
for i in range(1,50000000+1): | ||
curpos = (curpos+steps) % i + 1 | ||
if curpos == 1: | ||
va0 = i | ||
|
||
print("part 2", va0) |