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.
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]))
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
text/gemini; lang=en
This content has been proxied by September (ba2dc).