Skip to content

Commit

Permalink
[2024/19] p2 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 19, 2024
1 parent d981f9d commit 61ea803
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
Binary file modified .aoc_tiles/tiles/2024/19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 18 additions & 18 deletions 2024/19/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@


@cache
def validate(design: str, patterns: frozenset[str]) -> bool:
if design in patterns:
return True
else:
# try all splits
valid = False
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:
return True

return False
def validate(design: str, patterns: frozenset[str]) -> int:
# if there's nothing left of the design after recursively slicing patterns from it,
# we found a valid arrangement. return 1 to count it in our sum of all arrangements
if not design:
return 1

# try removing all patterns from the start of the design,
# then recursively do that for each remaining segment of the design
return sum(validate(design[len(pattern):], patterns)
for pattern in patterns if design[:len(pattern)] == pattern)


def main():
Expand All @@ -25,19 +21,23 @@ def main():
designs = designs.splitlines()

valid = 0
total_arrangements = 0
for design in designs:
if validate(design, patterns):
arrangements = validate(design, patterns)
if arrangements:
if aoc.args.verbose or aoc.args.progress:
print(f"1 - {design}")
print(f"1 - {design} - {arrangements}")
valid += 1
total_arrangements += arrangements
else:
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")
if aoc.args.verbose or aoc.args.progress:
print(f"validate() {validate.cache_info()}")

print(f"p1: {valid}")
print(f"p2: {total_arrangements}")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 196/488 ⭐
Advent of Code - 197/488 ⭐
</h1>
<h1 align="center">
2024 - 35 ⭐ - Python
2024 - 36 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down

0 comments on commit 61ea803

Please sign in to comment.