Skip to content

Instantly share code, notes, and snippets.

@maksverver
Created December 14, 2018 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maksverver/5e3527c6689c4e26f37ecc074b48c460 to your computer and use it in GitHub Desktop.
Save maksverver/5e3527c6689c4e26f37ecc074b48c460 to your computer and use it in GitHub Desktop.
Advent of Code 2018 day 14
from itertools import islice
import sys
def Generate():
scores = [3, 7]
yield scores[0]
yield scores[1]
i = 0
j = 1
while True:
x = scores[i] + scores[j]
if x < 10:
yield x
scores.append(x)
else:
y = x // 10
z = x % 10
yield y
yield z
scores.append(y)
scores.append(z)
i = (i + 1 + scores[i]) % len(scores)
j = (j + 1 + scores[j]) % len(scores)
def Part1(digits):
pos = int(digits)
return ''.join(map(str, islice(Generate(), pos, pos + 10)))
def Part2(digits):
target = int(digits)
modulus = 10**len(digits)
current = 0
for i, x in enumerate(Generate()):
if current == target:
return i - len(digits)
current = (10*current + x) % modulus
digits = sys.stdin.readline().strip()
print(Part1(digits))
print(Part2(digits))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment