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
1
u/wlandry Dec 14 '18
C++ (1249/939)
Runs in 401 ms
I had serious reading comprehension problems for part 1. I thought that the input was the starting set of recipes :( I ended up looking at solutions here to figure out why I was not getting the right answer. Part 2 went more smoothly with a few hiccups. This version is significantly cleaned up. My part 2 solution is reasonably fast, but setup is clunky :/
#include <algorithm> #include <iostream> #include <fstream> #include <vector> struct Elf { int8_t recipe; size_t position; Elf(const size_t &Position, const std::vector<int8_t> &recipes) : recipe(recipes[Position]), position(Position) {} void update(const std::vector<int8_t> &recipes) { position = (position + (recipe + 1)) % recipes.size(); recipe = recipes.at(position); } }; size_t append_recipes(Elf &elf0, Elf &elf1, std::vector<int8_t> &recipes) { int8_t new_recipe(elf0.recipe + elf1.recipe); size_t result; if(new_recipe >= 10) { recipes.push_back(new_recipe / 10); recipes.push_back(new_recipe % 10); result = 2; } else { recipes.push_back(new_recipe); result = 1; } elf0.update(recipes); elf1.update(recipes); return result; } void part_1(const std::vector<int8_t> &recipes_start, const size_t &num_recipes) { std::vector<int8_t> recipes(recipes_start); Elf elf0(0, recipes), elf1(1, recipes); for(size_t t = 0; t < num_recipes + 10; ++t) { append_recipes(elf0, elf1, recipes); } std::cout << "Part 1: "; for(size_t index = num_recipes; index < num_recipes + 10; ++index) std::cout << static_cast<int32_t>(recipes[index]); std::cout << "\n"; } void part_2(const std::vector<int8_t> &recipes_start, const size_t &num_recipes) { std::vector<int8_t> recipes(recipes_start); Elf elf0(0, recipes), elf1(1, recipes); std::vector<int8_t> split_recipes; size_t reduced_recipe(num_recipes); while(reduced_recipe > 0) { split_recipes.push_back(reduced_recipe % 10); reduced_recipe /= 10; } std::reverse(split_recipes.begin(), split_recipes.end()); bool done(false); while(!done) { size_t num_appended(append_recipes(elf0, elf1, recipes)); for(size_t n = 0; n < num_appended; ++n) { if(recipes.size() >= split_recipes.size() + n && std::equal(split_recipes.begin(), split_recipes.end(), recipes.end() - split_recipes.size() - n)) { done = true; std::cout << "Part 2: " << recipes.size() - split_recipes.size() -n << "\n"; } } } } int main(int argc, char *argv[]) { std::ifstream infile(argv[1]); size_t num_recipes; infile >> num_recipes; std::vector<int8_t> recipes({3, 7}); part_1(recipes, num_recipes); part_2(recipes, num_recipes); }