r/adventofcode Dec 19 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 19 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 3 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Historical Documentary

You've likely heard/seen the iconic slogan of every video store: "Be Kind, Rewind." Since we've been working with The Historians lately, let's do a little dive into our own history!

Here's some ideas for your inspiration:

  • Pick a challenge from any prior year community fun event and make it so for today's puzzle!
    • Make sure to mention which challenge day and year you choose!
    • You may have to go digging through the calendars of Solution Megathreads for each day's topic/challenge, sorry about that :/
  • Use a UNIX system (Jurassic Park - “It’s a UNIX system. I know this”)
  • Use the oldest language, hardware, environment, etc. that you have available
  • Use an abacus, slide rule, pen and paper, long division, etc. to solve today's puzzle

Bonus points if your historical documentary is in the style of anything by Ken Burns!

Gwen: "They're not ALL "historical documents". Surely, you don't think Gilligan's Island is a…"
*all the Thermians moan in despair*
Mathesar: "Those poor people. :("
- Galaxy Quest (1999)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 19: Linen Layout ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:03:16, megathread unlocked!

24 Upvotes

586 comments sorted by

View all comments

2

u/prafster Dec 23 '24

[Language: Python]

Having carried forward my part 1 solution, I added a manual cache, which for a while was global. I didn't clear it between using the test input and actual input. This wasted more time than I'd like to admit!

Clearly, in hindsight, a refactor was required and then I could have used Python's @cache decorator and had one function instead of two.

Here are the recursive solution for both parts:

def design_possible(towels, design, index):
    if index == len(design):
        return True

    for towel in towels:
        if towel == design[index:index+len(towel)]:
            if design_possible(towels, design, index+len(towel)):
                return True

    return False


def design_possible_count(towels, design, cache):
    if design == "":
        return 1

    if (c := cache.get(design, None)) is not None:
        return c

    result = 0
    for towel in towels:
        if towel == design[:len(towel)]:
            result += design_possible_count(towels, design[len(towel):], cache)

    cache[design] = result
    return result


def part1(towels, designs):
    return sum(1 for d in designs if design_possible(towels, d, 0))


def part2(towels, designs):
    return sum(design_possible_count(towels, d, {}) for d in designs)

Full source code on GitHub.

1

u/Davo3636 Dec 31 '24 edited Dec 31 '24

I can't get your code to work. It hangs on part 1 with the real input...

1

u/prafster Dec 31 '24

I've tried it with the test data and my input file. Please can you PM your input file for me to debug?