#!/usr/bin/env python3 import sys def memo(f): memo = {} def f2(x,y): if (x,tuple(y)) not in memo: memo[(x,tuple(y))] = f(x,tuple(y)) return memo[(x,tuple(y))] return f2 def fulfills(spring, cond): return [len(b) for b in spring.split('.') if len(b) > 0] == cond def dot(spring, cond): if len(spring) == 0 or spring[0] != '#': return solve_r(spring[1:], cond) else: return 0 def hashtag(spring, cond): c = cond[0] if len(spring) < c: return 0 for x in range(c): if spring[x] == '.': return 0 return dot(spring[c:], cond[1:]) @memo def solve_r(spring, cond): cond = [int(c) for c in cond] if cond == []: return 1 if spring.count('#') == 0 else 0 if not '?' in spring: return 1 if fulfills(spring, cond) else 0 if spring == '': return 0 return dot(spring, cond) + hashtag(spring, cond) if __name__ == '__main__': springs = [line.strip('\n').split(' ') for line in open(sys.argv[1])] res1 = 0 res2 = 0 for s in springs: res1 += solve_r(s[0], [int(c) for c in s[1].split(',')]) res2 += solve_r('?'.join([s[0]]*5),[int(c) for c in s[1].split(',')*5]) # challenge 1 res1 = str(res1) print(f"challenge 1:\n{res1}\n") # challenge 2 res2 = str(res2) print(f"challenge 2:\n{res2}\n")