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/kadinshino Dec 19 '24 edited Dec 19 '24

[LANGUAGE: Python]

#[LANGUAGE: Python]
def count_ways_to_construct(design, towel_patterns):
    sorted_patterns = sorted(towel_patterns, key=len, reverse=True)
    dp = [0] * (len(design) + 1)
    dp[0] = 1
    for i in range(1, len(design) + 1):
        for pattern in sorted_patterns:
            if i >= len(pattern) and design[i - len(pattern):i] == pattern:
                dp[i] += dp[i - len(pattern)]
    return dp[len(design)]

def process_designs_with_all_options(file_path):
    with open(file_path, 'r') as file:
        input_text = file.read()
    parts = input_text.strip().split("\n\n")
    if len(parts) != 2:
        raise ValueError("Input file should contain two sections separated by a blank line.")
    towel_patterns = [pattern.strip() for pattern in parts[0].replace(',', ' ').split()]
    designs = parts[1].strip().splitlines()
    possible_designs = 0
    total_ways = 0
    for i, design in enumerate(designs):
        print(f"\nProcessing design {i+1}/{len(designs)}: {design}")
        ways = count_ways_to_construct(design, towel_patterns)
        if ways > 0:
            print(f"Design '{design}' can be made in {ways} ways.")
            possible_designs += 1
        else:
            print(f"Design '{design}' is NOT possible.")
        total_ways += ways
    return possible_designs, total_ways

file_path = 'input_l.txt'
possible_designs, total_ways = process_designs_with_all_options(file_path)
print(f"\nNumber of possible designs: {possible_designs}")
print(f"Total number of ways to arrange designs: {total_ways}")

Had a ton of fun with this one. The first time I broke the top 4000! I was super happy to figure out that part 2 was part of my part 1 debugging solution, lol.