Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day12(s0, *, part2=False): # Dynamic programming, without numba.
- 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
- s += '.'
- # Dynamic programming over the string position and the counts position.
- num_matches = np.zeros((len(s) + 1, len(counts) + 1), int)
- num_matches[0, 0] = 1
- for s_index in range(len(s)):
- for counts_index in range(len(counts) + 1):
- if num := num_matches[s_index, counts_index]:
- if s[s_index] != '#':
- num_matches[s_index + 1, counts_index] += num
- if counts_index < len(counts):
- n = counts[counts_index]
- if s[s_index : s_index + n].replace('?', '#') == '#' * n and s[s_index + n] != '#':
- num_matches[s_index + n + 1, counts_index + 1] += num
- total += num_matches[-1, -1]
- return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement