diff --git a/answer_cache.json b/answer_cache.json index 05b308b..2b6c47c 100644 --- a/answer_cache.json +++ b/answer_cache.json @@ -130,5 +130,15 @@ ], "correct": "111,68" } + }, + "14": { + "1": { + "wrong": [], + "correct": "3718110721" + }, + "2": { + "wrong": [], + "correct": 20298300 + } } } \ No newline at end of file diff --git a/day14.py b/day14.py new file mode 100644 index 0000000..81aa345 --- /dev/null +++ b/day14.py @@ -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) diff --git a/inputs/input14 b/inputs/input14 new file mode 100644 index 0000000..e20fb9a --- /dev/null +++ b/inputs/input14 @@ -0,0 +1 @@ +306281 diff --git a/inputs/input14_test1 b/inputs/input14_test1 new file mode 100644 index 0000000..f11c82a --- /dev/null +++ b/inputs/input14_test1 @@ -0,0 +1 @@ +9 \ No newline at end of file diff --git a/inputs/input14_test2 b/inputs/input14_test2 new file mode 100644 index 0000000..5843a72 --- /dev/null +++ b/inputs/input14_test2 @@ -0,0 +1 @@ +92510 \ No newline at end of file diff --git a/inputs/input14_test3 b/inputs/input14_test3 new file mode 100644 index 0000000..79ec9cb --- /dev/null +++ b/inputs/input14_test3 @@ -0,0 +1 @@ +59414 \ No newline at end of file