Skip to content

Commit

Permalink
[2024/19] p1 rewritten without re (prep for p2)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 19, 2024
1 parent 82df799 commit d981f9d
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions 2024/19/script.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
from GhostyUtils import aoc
from functools import cache
import re


"""
@cache
def validate(design: str, patterns: frozenset[str]) -> bool:
if design in patterns:
if aoc.args.verbose or aoc.args.progress:
print(f"{design} - found")
return True
else:
# try all splits
valid = False
for i in range(1, len(design)-1):
for i in range(1, len(design)):
if aoc.args.verbose:
print(f" trying {design[:i]}-{design[i:]}")
valid = validate(design[:i], patterns) and validate(design[i:], patterns)
if valid:
break
return valid
return True

return False
"""


def validate(design: str, patterns: frozenset[str]) -> bool:
pattern = re.compile(r"^(" + r'|'.join(patterns) + r")+$")
return re.match(pattern, design)


def main():
Expand All @@ -44,6 +34,9 @@ def main():
if aoc.args.verbose or aoc.args.progress:
print(f"0 - {design}")

if aoc.args.progress:
print(f"cache: {validate.cache_info().hits} hits, {validate.cache_info().misses} misses")

print(f"p1: {valid}")


Expand Down

0 comments on commit d981f9d

Please sign in to comment.