Advertisement
hhoppe

Advent of code 2023 day 12

Dec 12th, 2023 (edited)
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. def day12(s0, *, part2=False, speedup=True):
  2.  
  3.   @functools.cache
  4.   def compute(s, counts):
  5.     if not s:
  6.       return 0 if counts else 1
  7.     if speedup and len(s) < len(counts) + sum(counts):
  8.       return 0
  9.     sum_matches = compute(s[1:], counts) if s[0] in '.?' else 0
  10.     if counts:
  11.       n = counts[0]
  12.       if s[:n].replace('?', '#') == '#' * n and s[n] in '.?':
  13.         sum_matches += compute(s[n + 1 :], counts[1:])
  14.     return sum_matches
  15.  
  16.   total = 0
  17.   for line in s0.splitlines():
  18.     s, right = line.split()
  19.     counts = tuple(map(int, right.split(',')))
  20.     if part2:
  21.       s = '?'.join([s] * 5)
  22.       counts *= 5
  23.     total += compute(s + '.', counts)
  24.  
  25.   return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement