From 70f8620419fd6e9f284ab6a50892012b8fd40be7 Mon Sep 17 00:00:00 2001 From: Simon Hessner Date: Sun, 17 Dec 2017 22:11:35 +0100 Subject: [PATCH] Add solution for day 17 --- day17/solution.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 day17/solution.py diff --git a/day17/solution.py b/day17/solution.py new file mode 100755 index 0000000..c107b4e --- /dev/null +++ b/day17/solution.py @@ -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) \ No newline at end of file