diff options
| author | mhsn <mail@mhsn.net> | 2026-03-18 21:48:13 +0000 |
|---|---|---|
| committer | mhsn <mail@mhsn.net> | 2026-03-18 21:48:13 +0000 |
| commit | 86bac31392a76da84817eec020d2b84d099b3cc1 (patch) | |
| tree | e2ee52db59b86b914d5b4bcceb19c9b5d899fff4 /2024/19/python.py | |
| parent | 62fe361fc42dea75deaf7ac31c0ba6ba80e26a9c (diff) | |
| download | puzzles-master.tar.gz puzzles-master.zip | |
Diffstat (limited to '2024/19/python.py')
| -rwxr-xr-x | 2024/19/python.py | 42 |
1 files changed, 0 insertions, 42 deletions
diff --git a/2024/19/python.py b/2024/19/python.py deleted file mode 100755 index efd3272..0000000 --- a/2024/19/python.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python3 - -from fileinput import input -from functools import cache -from itertools import takewhile - -inp = map(str.strip, input()) -towels = [t.strip() for t in next(takewhile(bool, inp)).split(",")] -designs = [d for d in inp if d] - -# Construct trie -trie = {} -for towel in towels: - prev = None - curr = trie - for t in towel: - term, succ = curr.setdefault(t, (False, {})) - prev = curr - curr = succ - prev[towel[-1]] = (True, curr) - - -@cache -def steps(design, n=0): - if n == len(design): - return 1 - ways = 0 - curr = trie - for d, s in enumerate(design[n:], start=1): - if s not in curr: - return ways - term, curr = curr[s] - if term: - ways += steps(design, n + d) - return ways - - -silver = sum(map(bool, map(steps, designs))) -gold = sum(map(steps, designs)) - -print("silver:", silver) -print("gold:", gold) |
