Skip to content

Commit

Permalink
Day 19: Python (p2)
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardob90 committed Dec 19, 2024
1 parent a95caa6 commit 4a8c905
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions 19/python/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ def insert(self, *patterns: str) -> None:
node = node.children[char]
node.is_end = True

def search(self, string: str, debug: bool = False) -> bool:
def _dp_search(self, string: str, count: bool = False) -> bool | int:
n = len(string)
dp = [True] + [False] * n
dp = [1] + [0] * n

for i in range(n):
if debug:
print(dp)

if not dp[i]:
continue

Expand All @@ -53,11 +50,16 @@ def search(self, string: str, debug: bool = False) -> bool:
node = node.children[string[j]]
j += 1
if node.is_end:
dp[j] = True
if j == n:
return True
dp[j] = dp[j] + dp[i] if count else True

return dp[n]

def search(self, string: str) -> bool:
return bool(self._dp_search(string, count=False))

def count(self, string: str) -> int:
return self._dp_search(string, count=True)


def parse_data(puzzle_input: str) -> tuple[list[str], list[str]]:
"""Parse input data"""
Expand All @@ -74,7 +76,9 @@ def part1(patterns: list[str], designs: list[str]) -> int:

def part2(patterns: list[str], designs: list[str]) -> int:
"""Solve part 2"""
return 0
trie = Trie()
trie.insert(*patterns)
return sum(trie.count(d) for d in designs)


def solve(puzzle_input: str) -> tuple[int, int]:
Expand Down

0 comments on commit 4a8c905

Please sign in to comment.