d12 classic dynamic programming, removing special python features

This commit is contained in:
Dory 2023-12-12 16:33:12 -08:00
parent 3169d08eb3
commit 25ad602e9a

View File

@ -2,7 +2,6 @@
# Of course all of this would have been cleaner and would perform the same if
# we were to use recursion with memoization (e.g. @memoize.cache)
import sys
import collections
import dataclasses
@dataclasses.dataclass(frozen=True)
@ -18,25 +17,28 @@ def no_dot_between(l, start, end):
def process(springs, counts):
# entries[(i,j)] = how many ways if we have only first i springs and j nums,
# given if that one was chosen to be working or no.
entries = collections.defaultdict(lambda: Entry(working=0, broken=0))
entries = {}
# boundary conditions, when there's no spring and no count left
# boundary conditions: when there's no spring and no count left, 1 choice
entries[(-1, -1)] = Entry(working=1, broken=0)
# boundary conditions, when there's no count left but still springs
# boundary conditions: when there's no count left but still springs left
for i in range(len(springs)):
entries[(i, -1)] = Entry(
working=(entries[(i - 1, -1)].working
if springs[i] in (".", "?")
else 0),
broken=0)
# boundary conditions: when there's no springs left but there's still count
for j in range(len(counts)):
entries[(-1, j)] = Entry(working=0, broken=0)
# building the rest of the table
for i in range(len(springs)):
for j in range(len(counts)):
prev_entry_if_working = entries[(i - 1, j)]
prev_working_entry_if_broken = (
entries[(i - counts[j], j - 1)].working
if no_dot_between(springs, i-counts[j], i)
entries[(i-counts[j], j-1)].working
if i-counts[j] >= -1 and no_dot_between(springs, i-counts[j], i)
else 0)
if springs[i] == ".":