-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c82521a
commit bf4d270
Showing
4 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
r, wr, b, g, bwu, rb, gb, br | ||
|
||
brwrr | ||
bggr | ||
gbbr | ||
rrbgbr | ||
ubwu | ||
bwurrg | ||
brgr | ||
bbrgwb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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): | ||
valid = validate(design[:i], patterns) and validate(design[i:], patterns) | ||
if valid: | ||
break | ||
return valid | ||
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(): | ||
patterns, designs = aoc.read_sections() | ||
patterns = frozenset(patterns.split(', ')) | ||
designs = designs.splitlines() | ||
|
||
valid = 0 | ||
for design in designs: | ||
if validate(design, patterns): | ||
if aoc.args.verbose or aoc.args.progress: | ||
print(f"1 - {design}") | ||
valid += 1 | ||
else: | ||
if aoc.args.verbose or aoc.args.progress: | ||
print(f"0 - {design}") | ||
|
||
print(f"p1: {valid}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters