Solve 2018 day 14
This commit is contained in:
62
2018/d14.py
Normal file
62
2018/d14.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
def part_1(data):
|
||||||
|
num = int(data.strip())
|
||||||
|
recipes = [3, 7]
|
||||||
|
i, j = 0, 1
|
||||||
|
|
||||||
|
while True:
|
||||||
|
s = recipes[i] + recipes[j]
|
||||||
|
ss = str(s)
|
||||||
|
if len(ss) == 1:
|
||||||
|
recipes.append(s)
|
||||||
|
elif len(ss) == 2:
|
||||||
|
recipes.append(int(ss[0]))
|
||||||
|
recipes.append(int(ss[1]))
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
i = (i + 1 + recipes[i]) % len(recipes)
|
||||||
|
j = (j + 1 + recipes[j]) % len(recipes)
|
||||||
|
if i == j:
|
||||||
|
j = (j + 1) % len(recipes)
|
||||||
|
if len(recipes) > num + 10:
|
||||||
|
print("".join(map(str, recipes[num : num + 10])))
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
def part_2(data):
|
||||||
|
target = list(map(int, data.strip()))
|
||||||
|
recipes = [3, 7]
|
||||||
|
|
||||||
|
i, j = 0, 1
|
||||||
|
while True:
|
||||||
|
s = recipes[i] + recipes[j]
|
||||||
|
if s < 10:
|
||||||
|
recipes.append(s)
|
||||||
|
elif s < 100:
|
||||||
|
recipes.append(s // 10)
|
||||||
|
recipes.append(s % 10)
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
i = (i + 1 + recipes[i]) % len(recipes)
|
||||||
|
j = (j + 1 + recipes[j]) % len(recipes)
|
||||||
|
if i == j:
|
||||||
|
j = (j + 1) % len(recipes)
|
||||||
|
|
||||||
|
if recipes[-len(target) :] == target:
|
||||||
|
print(len(recipes) - len(target))
|
||||||
|
elif recipes[-len(target) - 1 : -1] == target:
|
||||||
|
print(len(recipes) - len(target) - 1)
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
input_file = __file__.replace(".py", ".txt")
|
||||||
|
with open(input_file) as f:
|
||||||
|
data = f.read()
|
||||||
|
part_1(data)
|
||||||
|
part_2(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user