This commit is contained in:
Stefan Harmuth 2023-11-29 07:31:32 +01:00
parent d0e3be3296
commit 1a8d7ccf96
6 changed files with 118 additions and 0 deletions

View File

@ -130,5 +130,15 @@
],
"correct": "111,68"
}
},
"14": {
"1": {
"wrong": [],
"correct": "3718110721"
},
"2": {
"wrong": [],
"correct": 20298300
}
}
}

104
day14.py Normal file
View File

@ -0,0 +1,104 @@
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)

1
inputs/input14 Normal file
View File

@ -0,0 +1 @@
306281

1
inputs/input14_test1 Normal file
View File

@ -0,0 +1 @@
9

1
inputs/input14_test2 Normal file
View File

@ -0,0 +1 @@
92510

1
inputs/input14_test3 Normal file
View File

@ -0,0 +1 @@
59414