|
|
@ -1,6 +1,7 @@ |
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
|
import sys, re |
|
|
|
import sys |
|
|
|
import regex as re |
|
|
|
from itertools import combinations |
|
|
|
from more_itertools import locate |
|
|
|
|
|
|
@ -19,6 +20,7 @@ def fulfills(spring, cond): |
|
|
|
|
|
|
|
|
|
|
|
def solve_row1(spring, cond): |
|
|
|
if len(spring) == (sum(cond) + len(cond) - 1): return 1 |
|
|
|
if fulfills(spring, cond): return 1 |
|
|
|
sols = 0 |
|
|
|
not_dam = spring.count('?') + spring.count('#') - sum(cond) |
|
|
@ -37,8 +39,10 @@ def solve_r(spring, cond): |
|
|
|
if cond == '': return 1 |
|
|
|
cond = [int(c) for c in cond.split(',')] |
|
|
|
spring = spring.strip('.') |
|
|
|
inds = list(locate(spring.strip('.'), lambda x: x == '.')) |
|
|
|
if inds == []: return solve_row1(spring, cond) |
|
|
|
inds = list(locate(spring, lambda x: x == '.')) |
|
|
|
if inds == []: |
|
|
|
if len(spring) > 10: print(spring, cond) |
|
|
|
return solve_row1(spring, cond) |
|
|
|
i = inds[0] |
|
|
|
sols = 0 |
|
|
|
for j in range(0, len(cond)): |
|
|
@ -48,28 +52,14 @@ def solve_r(spring, cond): |
|
|
|
return sols |
|
|
|
|
|
|
|
|
|
|
|
def solve_reg(spring, cond): |
|
|
|
reg = r'^(\.|\?)*' |
|
|
|
cond = [int(c) for c in cond.split(',')] |
|
|
|
for i, z in enumerate(cond): |
|
|
|
reg += '(\?|\#){' + f'{z}' + '}' |
|
|
|
if i < (len(cond)-1): reg += '(\.|\?)+' |
|
|
|
reg += '(\.|\?)*$' |
|
|
|
print(reg, [i for i in re.findall(reg, spring, overlapped = True)]) |
|
|
|
return sum([1 for i in re.finditer(reg, spring)]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
springs = [line.strip('\n').split(' ') for line in open(sys.argv[1])] |
|
|
|
res1 = 0 |
|
|
|
res2 = 0 |
|
|
|
for s in springs[0:2]: |
|
|
|
# res1 += solve_row1(*s) |
|
|
|
res1 += solve_reg(*s) |
|
|
|
# print(solve_row1(*s)) |
|
|
|
# solve_reg('?'.join([s[0]] * 5), ','.join([s[1]] * 5)) |
|
|
|
print(res1, s) |
|
|
|
for s in springs: |
|
|
|
res1 += solve_r(*s) |
|
|
|
res2 += solve_r('?'.join([s[0]]*5),','.join((s[1].split(','))*5)) |
|
|
|
print(solve_r('?'.join([s[0]]*5),','.join((s[1].split(','))*5))) |
|
|
|
|
|
|
|
# challenge 1 |
|
|
|
res1 = str(res1) |
|
|
|