Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @numba.njit
- def day12_num_matches(s, counts):
- num_matches = np.zeros((len(s) + 1, len(counts) + 1), np.int64)
- num_matches[0, 0] = 1
- for s_index in range(len(s)):
- for counts_index in range(len(counts) + 1):
- num = num_matches[s_index, counts_index]
- if num:
- if s[s_index] != ord('#'):
- num_matches[s_index + 1, counts_index] += num
- if counts_index < len(counts):
- n = counts[counts_index]
- if s_index + n < len(s) and s[s_index + n] != ord('#'):
- matches_word = True
- for i in range(s_index, s_index + n):
- if s[i] == ord('.'):
- matches_word = False
- break
- if matches_word:
- num_matches[s_index + n + 1, counts_index + 1] += num
- return num_matches[-1, -1]
- def day12(s0, *, part2=False):
- 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_array = np.array([ord(ch) for ch in list(s + '.')], np.uint8)
- total += day12_num_matches(s_array, np.array(counts))
- return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement