2018/Day14/Day14B.py

45 lines
999 B
Python

from sys import argv, exit
from tqdm import tqdm
from itertools import count
try:
input_ = bytes([int(x) for x in argv[1]])
l = len(input_)
except IndexError:
print(f"Usage: {argv[0]} [input]")
exit(1)
recipes = bytearray([3, 7])
a, b = 0, 1
def print_recipes():
for i, recipe in enumerate(recipes):
if i == a:
print(f"({recipe:>2d})", end=" ")
elif i == b:
print(f"[{recipe:>2d}]", end=" ")
else:
print(f" {recipe:>2d} ", end=" ")
print()
for _ in tqdm(count()):
next_ = recipes[a] + recipes[b]
digits = bytearray([])
if next_ == 0:
digits.append(0)
else:
while next_:
digits.append(next_ % 10)
next_ //= 10
recipes.extend(digits[::-1])
a = (a + recipes[a] + 1) % len(recipes)
b = (b + recipes[b] + 1) % len(recipes)
if recipes[-l - 2:-2] == input_ or recipes[-l - 1:-1] == input_:
print(recipes.find(input_))
break