Advent of Code 2024, Day 19

I like how you only had to replace the disjunction with an addition for part 2 (which, in some notations, even use the same symbol, namely the plus sign).

You could also replace the loop in my helper function with a list comprehension and then apply “any” or “sum” to it, but I find the loop easier to read.

Part 1

import sys

def check(d, available):
    if d == "": return True
    if d in mem: return mem[d]
    r = False
    for a in available:
        if not d.startswith(a): continue
        r |= check(d[len(a):], available)
    mem[d] = r
    return r

mem = {}
desired = []
with open(sys.argv[1]) as f:
    for line in f:
        line = line.strip()
        if "," in line:
            available = set(line.split(", "))
        elif line != "":
            desired.append(line)

print(sum([check(d, available) for d in desired]))

Part 2

import sys

def check(d, available):
    if d == "": return 1
    if d in mem: return mem[d]
    r = 0
    for a in available:
        if not d.startswith(a): continue
        r += check(d[len(a):], available)
    mem[d] = r
    return r

mem = {}
desired = []
with open(sys.argv[1]) as f:
    for line in f:
        line = line.strip()
        if "," in line:
            available = set(line.split(", "))
        elif line != "":
            desired.append(line)

print(sum([check(d, available) for d in desired]))

2024-12-19

Proxy Information
Original URL
gemini://dkalak.de/aoc/19.gmi
Status Code
Success (20)
Meta
text/gemini; lang=en
Capsule Response Time
114.685305 milliseconds
Gemini-to-HTML Time
0.33236 milliseconds

This content has been proxied by September (ba2dc).