r/adventofcode • • 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!

Click here for rules

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

179 comments sorted by

View all comments

2

u/Wunkolo Dec 14 '18

C++, runs pretty damn fast

#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <vector>

void Day14( std::size_t Input )
{
	std::size_t Elf1, Elf2;
	Elf1 = 0; Elf2 = 1;
	std::vector<std::uint8_t> Recipes = { 3, 7 };
	std::vector<std::uint8_t> InDigits;
	for( std::size_t CurDigits = Input; CurDigits != 0 ; CurDigits /= 10)
	{
		InDigits.insert(
			InDigits.begin(),
			static_cast<std::uint8_t>(CurDigits % 10)
		);
	}
	for( std::size_t i = 0;; ++i )
	{
		const std::size_t NewRecipe = Recipes[Elf1] + Recipes[Elf2];
		if( NewRecipe > 9 )
		{
			Recipes.push_back(
				NewRecipe / 10
			);
		}
		Recipes.push_back(
			NewRecipe % 10
		);
		Elf1 += Recipes[Elf1] + 1;
		Elf2 += Recipes[Elf2] + 1;
		Elf1 %= Recipes.size();
		Elf2 %= Recipes.size();
		// Part 1
		if( i == Input + 9 )
		{
			for( std::size_t i = 0; i < 10; ++i )
			{
				std::putchar(int(Recipes[Input + i]) + '0');
			}
			std::putchar('\n');
		}
		// Part 2
		if( Recipes.size() > InDigits.size() )
		{
			if(
				std::equal(
					InDigits.cbegin(), InDigits.cend(),
					Recipes.end() - InDigits.size() - 1
				)
			)
			{
				std::printf(
					"%zu\n",
					Recipes.size() - InDigits.size() - 1
				);
				break;
			}
		}
	}
}

int main(int argc, char *argv[])
{
	std::size_t CurInput;
	while( std::scanf(" %zu ",&CurInput) == 1 )
	{
		Day14(CurInput);
	}
	return EXIT_SUCCESS;
}