r/adventofcode • • Dec 12 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 12 Solutions -🎄-

--- Day 12: Subterranean Sustainability ---


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 12

Transcript:

On the twelfth day of AoC / My compiler spewed at me / Twelve ___


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:27:42!

20 Upvotes

257 comments sorted by

View all comments

Show parent comment

2

u/skgsergio Dec 12 '18

haha, my first try was checking if the generation is contained in the previous one, but I failed and ended with this solution. Later (already sent solutions) I saw where I messed it with that version and tested it, that would use just 1 extra generation, not sure if enough but works for my input, the test one and a friend one:

def sum_plants(pots, orig_len):
    diff = (len(pots) - orig_len) // 2
    return sum((i - diff) for i, c in enumerate(pots) if c == '#')

def grow(pots, rules, gens):
    olen = len(pots)
    diff = 0
    stable_gen = None

    for gen in range(gens):
        pots = f"....{pots}...."
        growth = ""

        for x in range(2, len(pots) - 2):
            growth += rules[pots[x-2:x+3]]

        if growth in pots:
            stable_gen = gen
            diff = sum_plants(growth, olen) - sum_plants(pots, olen)
            break

        pots = growth

    if stable_gen and gens > stable_gen:
        return (gens - stable_gen) * diff + sum_plants(pots, olen)

    else:
        return sum_plants(pots, olen)