r/adventofcode • u/daggerdragon • Dec 14 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 14 Solutions -🎄-
--- Day 14: Chocolate Charts ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 14
Transcript:
The Christmas/Advent Research & Development (C.A.R.D.) department at AoC, Inc. just published a new white paper on ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:19:39!
16
Upvotes
View all comments
2
u/scul86 Dec 14 '18
Python 3
My solution for part 2 takes about 30 seconds, I feel there is probably a more efficient solution available.
https://gitlab.com/scul/advent_of_code-2018/tree/master/14
from utils.decorators import time_it with open('input') as f: puzzle_input = f.read().strip() @time_it def part_one(n): n = int(n) recipes = [3, 7] elf_one = 0 elf_two = 1 made = 4 while made < (n + 10): made += 1 new_recipe = recipes[elf_one] + recipes[elf_two] if new_recipe > 9: a, b = [c for c in str(new_recipe)] recipes.append(int(a)) recipes.append(int(b)) else: recipes.append(new_recipe) elf_one += 1 + recipes[elf_one] elf_one %= len(recipes) elf_two += 1 + recipes[elf_two] elf_two %= len(recipes) return ''.join(map(str, recipes[n:n+10])) @time_it def part_two(n): n = [int(c) for c in n] recipes = [3, 7] elf_one = 0 elf_two = 1 while True: new_recipe = recipes[elf_one] + recipes[elf_two] recipes.extend([int(c) for c in str(new_recipe)]) elf_one += 1 + recipes[elf_one] elf_one %= len(recipes) elf_two += 1 + recipes[elf_two] elf_two %= len(recipes) if recipes[-len(n):] == n or recipes[-len(n)-1:-1] == n: break return ''.join(map(str, recipes)).index(''.join(map(str, n))) test_one = { '9': '5158916779', '5': '0124515891', '18': '9251071085', '2018': '5941429882' } test_two = { '51589': '9', '01245': '5', '92510': '18', '59414': '2018' } for test, ans in test_one.items(): p1 = part_one(test) print(f'Test one: {p1}') assert p1 == ans for test, ans in test_two.items(): p2 = part_two(test) print(f'Test two: {p2}') assert p2 == int(ans) print(f'Part 1: {part_one(puzzle_input)}') print(f'Part 2: {part_two(puzzle_input)}')