aboutsummaryrefslogtreecommitdiff
path: root/2023/12/solve.py
diff options
context:
space:
mode:
authorMarvin Borner2023-12-12 12:20:11 +0100
committerMarvin Borner2023-12-12 12:20:11 +0100
commit0aeee892938893b61f5a12df52d1dddbfe68e1c4 (patch)
treed384c21eb5ed3b271ec253d7c438872eef769b80 /2023/12/solve.py
parent49f1df9dc23f7e1e25a1fa9978c0ad9f3b169320 (diff)
No cache
Diffstat (limited to '2023/12/solve.py')
-rw-r--r--2023/12/solve.py58
1 files changed, 36 insertions, 22 deletions
diff --git a/2023/12/solve.py b/2023/12/solve.py
index bd9da6d..da0efc1 100644
--- a/2023/12/solve.py
+++ b/2023/12/solve.py
@@ -4,31 +4,37 @@ L = [
]
-def groups(pat):
- gs = []
- c = []
- for p in pat:
- if p in "#?":
- c.append(p)
- elif c:
- gs.append(c)
- c = []
- return gs[::-1]
+# look mum no recursion!! :OOO
+def arrangements(pat, sol):
+ arr = 0
+ done = [0]
+ sols = [sol]
+ pats = [pat]
+ current = (tuple(sol),pat)
-def arrangements(pat, sol):
- arr = 1
+ while pats or sols:
+ p = pats.pop()
+ s = sols.pop()
+ d = done.pop()
- gs = groups(pat)
- for i, g in enumerate(gs):
- if len(g) == sol[0]: # ???.[#??] 1,1,[3]
- del sol[0]
+ if not p:
+ arr += 1 if not s and not d else 0
continue
- if g.count("#") == 0:
- if len(g) == len(sol) + 1 and len(sol) == sum(sol): # [???].### [1,1],3
- break
- # if len(g) > len(sol)
- print(g, sol)
+
+ for ch in "#." if p[0] == "?" else p[0]:
+ if ch == '#':
+ sols.append(s)
+ pats.append(p[1:])
+ done.append(d + 1)
+ elif not d:
+ sols.append(s)
+ pats.append(p[1:])
+ done.append(0)
+ elif s and s[0] == d:
+ sols.append(s[1:])
+ pats.append(p[1:])
+ done.append(0)
return arr
@@ -36,8 +42,16 @@ def arrangements(pat, sol):
def part1():
res = 0
for pat, sol in L:
- res += arrangements(f".{pat}.", sol[::-1])
+ res += arrangements(f".{pat}.", sol)
+ print(res)
+
+
+def part2():
+ res = 0
+ for pat, sol in L:
+ res += arrangements("?".join([pat] * 5) + ".", sol * 5)
print(res)
part1()
+# part2()