38 lines
921 B
Python
38 lines
921 B
Python
|
import sys
|
||
|
|
||
|
def valid(pat:str, nums:list[int]):
|
||
|
pat += '.'
|
||
|
c = 0
|
||
|
for i,p in enumerate(pat):
|
||
|
if p == '#':
|
||
|
c += 1
|
||
|
else:
|
||
|
if c>0:
|
||
|
if c == nums[0]:
|
||
|
c = 0
|
||
|
nums = nums[1:]
|
||
|
if len(nums) == 0:
|
||
|
return 1 if pat[i:].find('#')<0 else 0
|
||
|
else:
|
||
|
return 0
|
||
|
return 0
|
||
|
|
||
|
def next(pattern, nums):
|
||
|
i = pattern.find('?')
|
||
|
if i < 0:
|
||
|
v = valid(pattern, nums)
|
||
|
if v == 1:
|
||
|
print("valid", pattern)
|
||
|
return v
|
||
|
else:
|
||
|
return next(pattern[:i]+'.'+pattern[i+1:], nums)+next(pattern[:i]+'#'+pattern[i+1:], nums)
|
||
|
|
||
|
S= 0
|
||
|
for i,l in enumerate(sys.stdin.read().splitlines()):
|
||
|
pattern, snums = l.split(' ')
|
||
|
nums = list(map(int,snums.split(',')))
|
||
|
n = next(pattern, nums)
|
||
|
print(i,l,n)
|
||
|
S += n
|
||
|
print(S)
|