aoc2018/day14.py
2023-11-29 07:31:32 +01:00

105 lines
2.4 KiB
Python

from __future__ import annotations
from tools.aoc import AOCDay
from typing import Any
class Recipe:
score: int
next: Recipe
def __init__(self, score: int):
self.score = score
class RecipeList:
def __init__(self):
self.head = Recipe(3)
self.tail = Recipe(7)
self.head.next = self.tail
self.tail.next = self.head
def add(self, score: int):
r = Recipe(score)
r.next = self.head
self.tail.next = r
self.tail = r
def print(self):
m = self.head
while m.next != self.head:
print("%d " % m.score, end="")
m = m.next
print(m.score)
class Day(AOCDay):
inputs = [
[
("5158916779", "input14_test1"),
("3718110721", "input14"),
],
[
(18, "input14_test2"),
(2018, "input14_test3"),
(20298300, "input14"),
],
]
def part1(self) -> Any:
recipes = RecipeList()
elf_1 = recipes.head
elf_2 = recipes.tail
for _ in range(self.getInput(int) + 10):
new_score = str(elf_1.score + elf_2.score)
for c in map(int, new_score):
recipes.add(c)
for i in range(elf_1.score + 1):
elf_1 = elf_1.next
for i in range(elf_2.score + 1):
elf_2 = elf_2.next
mark = recipes.head
for _ in range(self.getInput(int)):
mark = mark.next
res = ""
for _ in range(10):
res += str(mark.score)
mark = mark.next
return res
def part2(self) -> Any:
recipes = RecipeList()
elf_1 = recipes.head
elf_2 = recipes.tail
count = 2
target = self.getInput()
current = "0" * (len(target) - 2) + "37"
while current != target:
new_score = str(elf_1.score + elf_2.score)
for c in map(int, new_score):
recipes.add(c)
count += 1
current = current[1:] + str(c)
if current == target:
return count - len(target)
for i in range(elf_1.score + 1):
elf_1 = elf_1.next
for i in range(elf_2.score + 1):
elf_2 = elf_2.next
return count - len(target)
if __name__ == "__main__":
day = Day(2018, 14)
day.run(verbose=True)