Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2018-12-14 - Chocolate Charts

(original .ipynb)
# Part 1
recipes = [3, 7]

elves = list([ n for n in range(2) ])

def ten_recipes_after(max_iterations):
    global recipes
    global elves
    
    while len(recipes) < (10 + max_iterations):
        # generate the new recipes
        new_recipes_str = str(sum([ recipes[elf_recipe] for elf_recipe in elves ]))
        new_recipes = [ int(c) for c in new_recipes_str ]
        recipes += new_recipes

        # move the elves
        for elf_index, current_recipe in enumerate(elves):
            new_elf_recipe = (current_recipe + 1 + recipes[current_recipe]) % len(recipes)
            elves[elf_index] = new_elf_recipe

    return int("".join([ str(i) for i in recipes[-10:]]))

print(ten_recipes_after(165061))
5992684592
# Part 2
def first_instance_of(pattern_str):
    pattern = [ int(c) for c in pattern_str ]    
    recipes = [3, 7]
    elves = list([ n for n in range(2) ])
    count = 0
    pattern_found = False
    
    while not pattern_found:
        # generate the new recipes
        new_recipes_str = str(sum([ recipes[elf_recipe] for elf_recipe in elves ]))
        new_recipes = [ int(c) for c in new_recipes_str ]
        recipes += new_recipes

        # move the elves
        for elf_index, current_recipe in enumerate(elves):
            new_elf_recipe = (current_recipe + 1 + recipes[current_recipe]) % len(recipes)
            elves[elf_index] = new_elf_recipe

        if recipes[-len(pattern):] == pattern:
            pattern_found = True
            
        count += 1
    return ("".join([ str(i) for i in recipes]).index(pattern_str))

assert first_instance_of("51589") == 9
assert first_instance_of("01245") == 5
assert first_instance_of("92510") == 18
assert first_instance_of("59414") == 2018

print(first_instance_of("165061"))
OK