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

Show parent comment

1

u/aoc-fan Dec 15 '18

Check my TS Solution

const findScore = (iterations: number) => {
    const recipeBoard = [3, 7];
    let [firstElf, secondElf] = [0, 1];
    while (recipeBoard.length < (iterations + 10)) {
        const firstElfRecipe = recipeBoard[firstElf];
        const secondElfRecipe = recipeBoard[secondElf];
        const newRecipe = firstElfRecipe + secondElfRecipe;
        if ( newRecipe > 9) {
            recipeBoard.push(1);
            recipeBoard.push(newRecipe - 10);
        } else {
            recipeBoard.push(newRecipe);
        }
        firstElf = (firstElf + firstElfRecipe + 1) % recipeBoard.length;
        secondElf = (secondElf + secondElfRecipe + 1) % recipeBoard.length;
    }
    return recipeBoard.slice(iterations, iterations + 10).join("");
};

const findRecipesToSequence = (sequence: string) => {
    const recipeBoard = [3, 7];
    let [firstElf, secondElf, last] = [0, 1, ""];
    while (true) {
        const firstElfRecipe = recipeBoard[firstElf];
        const secondElfRecipe = recipeBoard[secondElf];
        const newRecipe = firstElfRecipe + secondElfRecipe;
        if (newRecipe > 9) {
            recipeBoard.push(1);
            recipeBoard.push(newRecipe - 10);
        } else {
            recipeBoard.push(newRecipe);
        }
        firstElf = (firstElf + firstElfRecipe + 1) % recipeBoard.length;
        secondElf = (secondElf + secondElfRecipe + 1) % recipeBoard.length;

        last = last + newRecipe;
        const foundIndex = last.indexOf(sequence);
        if (foundIndex > -1) {
            return recipeBoard.length - last.length + foundIndex;
        } else if (last.length > sequence.length) {
            last = last.slice(last.length - sequence.length);
        }
    }
};