Solve 2018 day 14

This commit is contained in:
felixm 2024-07-13 12:09:54 -04:00
parent 5e68e08807
commit 9346e3f4d0
2 changed files with 64 additions and 1 deletions

62
2018/d14.py Normal file
View 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()

View File

@ -102,7 +102,8 @@ written in Python.
- Day 11: 22:52
- Day 12: 180:00
- Day 13: 73:09
- Day 14:
- Day 14: 20:48
- Day 15:
# 2022