Skip to content

19

https://adventofcode.com/2024/day/19

prob1

python
import sys

prefix_list = sys.stdin.readline().strip().split(', ')
print(prefix_list)

cnt = 0
for line in sys.stdin:
    line = line.strip()
    if not line:
        continue
    tab = [False]*(len(line)+1)
    tab[0] = True
    for ptr in range(len(line)):
        if not tab[ptr]:
            continue

        for prefix in prefix_list:
            if line[ptr:].startswith(prefix):
                tab[ptr+len(prefix)] = True

    if tab[-1]:
        cnt += 1

print(cnt)

prob2

python
import sys

prefix_list = sys.stdin.readline().strip().split(', ')
print(prefix_list)

cnt = 0
for line in sys.stdin:
    line = line.strip()
    if not line:
        continue
    tab = [0]*(len(line)+1)
    tab[0] = 1
    for ptr in range(len(line)):
        if not tab[ptr]:
            continue

        for prefix in prefix_list:
            if line[ptr:].startswith(prefix):
                tab[ptr+len(prefix)] += tab[ptr]
    cnt += tab[-1]

print(cnt)

Changelog

Just observe 👀