d12 classic dynamic programming, removing special python features
This commit is contained in:
parent
3169d08eb3
commit
25ad602e9a
12
12/part2.py
12
12/part2.py
@ -2,7 +2,6 @@
|
|||||||
# Of course all of this would have been cleaner and would perform the same if
|
# 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)
|
# we were to use recursion with memoization (e.g. @memoize.cache)
|
||||||
import sys
|
import sys
|
||||||
import collections
|
|
||||||
import dataclasses
|
import dataclasses
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
@ -18,17 +17,20 @@ def no_dot_between(l, start, end):
|
|||||||
def process(springs, counts):
|
def process(springs, counts):
|
||||||
# entries[(i,j)] = how many ways if we have only first i springs and j nums,
|
# 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.
|
# 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)
|
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)):
|
for i in range(len(springs)):
|
||||||
entries[(i, -1)] = Entry(
|
entries[(i, -1)] = Entry(
|
||||||
working=(entries[(i - 1, -1)].working
|
working=(entries[(i - 1, -1)].working
|
||||||
if springs[i] in (".", "?")
|
if springs[i] in (".", "?")
|
||||||
else 0),
|
else 0),
|
||||||
broken=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
|
# building the rest of the table
|
||||||
for i in range(len(springs)):
|
for i in range(len(springs)):
|
||||||
@ -36,7 +38,7 @@ def process(springs, counts):
|
|||||||
prev_entry_if_working = entries[(i - 1, j)]
|
prev_entry_if_working = entries[(i - 1, j)]
|
||||||
prev_working_entry_if_broken = (
|
prev_working_entry_if_broken = (
|
||||||
entries[(i-counts[j], j-1)].working
|
entries[(i-counts[j], j-1)].working
|
||||||
if no_dot_between(springs, i-counts[j], i)
|
if i-counts[j] >= -1 and no_dot_between(springs, i-counts[j], i)
|
||||||
else 0)
|
else 0)
|
||||||
|
|
||||||
if springs[i] == ".":
|
if springs[i] == ".":
|
||||||
|
Loading…
Reference in New Issue
Block a user