From 312113ce81cd49471ad617e33ac7d8355eb009d2 Mon Sep 17 00:00:00 2001 From: Maya Herrscher Date: Fri, 15 Dec 2023 23:06:55 +0100 Subject: [PATCH] Bene do stuff --- 2023/day12/code.py | 55 +++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/2023/day12/code.py b/2023/day12/code.py index bd482dd..57d3e88 100755 --- a/2023/day12/code.py +++ b/2023/day12/code.py @@ -1,17 +1,14 @@ #!/usr/bin/env python3 import sys -import regex as re -from itertools import combinations -from more_itertools import locate def memo(f): memo = {} def f2(x,y): - if (x,y) not in memo: memo[(x,y)] = f(x,y) - return memo[(x,y)] + if (x,tuple(y)) not in memo: memo[(x,tuple(y))] = f(x,tuple(y)) + return memo[(x,tuple(y))] return f2 @@ -19,37 +16,26 @@ def fulfills(spring, cond): return [len(b) for b in spring.split('.') if len(b) > 0] == 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) - if not_dam < 1: return 0 - inds = list(locate(spring, lambda x: x == '?')) - for c in combinations(inds, not_dam): - rs = list(spring) - for i in c: - rs[i] = '.' - if fulfills(''.join(rs),cond): sols += 1 - return sols +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): - if cond == '': return 1 - cond = [int(c) for c in cond.split(',')] - spring = spring.strip('.') - 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)): - if sum(cond[:j]) <= len(list(locate(spring[:i], lambda x: x != '.'))) and sum(cond[j:]) <= len(list(locate(spring[i:], lambda x: x != '.'))): - cond1, cond2 = ','.join([str(c) for c in cond[:j]]), ','.join([str(c) for c in cond[j:]]) - sols += solve_r(spring[:i], cond1) * solve_r(spring[i+1:], cond2) - return sols + 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__': @@ -57,9 +43,8 @@ if __name__ == '__main__': res1 = 0 res2 = 0 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))) + 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)