Created
December 14, 2018 12:48
-
-
Save maksverver/5e3527c6689c4e26f37ecc074b48c460 to your computer and use it in GitHub Desktop.
Advent of Code 2018 day 14
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
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