Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day12(s0, *, part2=False, speedup=True):
- @functools.cache
- def compute(s, counts):
- if not s:
- return 0 if counts else 1
- if speedup and len(s) < len(counts) + sum(counts):
- return 0
- sum_matches = compute(s[1:], counts) if s[0] in '.?' else 0
- if counts:
- n = counts[0]
- if s[:n].replace('?', '#') == '#' * n and s[n] in '.?':
- sum_matches += compute(s[n + 1 :], counts[1:])
- return sum_matches
- total = 0
- for line in s0.splitlines():
- s, right = line.split()
- counts = tuple(map(int, right.split(',')))
- if part2:
- s = '?'.join([s] * 5)
- counts *= 5
- total += compute(s + '.', counts)
- return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement