re: AoC Day 14: Chocolate Charts VIEW POST

FULL DISCUSSION
 

I liked this one quite a bit too!

#!/usr/bin/env python

NUM_RECIPES = '170641'


def run(check_success, grab_info):
    recipes = '37'
    elf_one = 0
    elf_two = 1

    while True:
        elf_one_score = int(recipes[elf_one])
        elf_two_score = int(recipes[elf_two])

        recipes += str(elf_one_score + elf_two_score)
        num_recipes = len(recipes)
        elf_one += 1 + elf_one_score
        elf_one %= num_recipes
        elf_two += 1 + elf_two_score
        elf_two %= num_recipes

        if check_success(recipes):
            print(grab_info(recipes))
            break


if __name__ == '__main__':
    # Part 1
    run(
        lambda recipes: len(recipes) >= 10 + int(NUM_RECIPES),
        lambda recipes: recipes[int(NUM_RECIPES):int(NUM_RECIPES) + 10],
    )

    # Part 2
    run(
        lambda recipes: NUM_RECIPES in recipes[-len(NUM_RECIPES) - 10:],
        lambda recipes: recipes.index(NUM_RECIPES),
    )
code of conduct - report abuse