Skip to content

Commit bf4d270

Browse files
committed
[2024/19] p1 solved
1 parent c82521a commit bf4d270

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

.aoc_tiles/tiles/2024/19.png

5.08 KB
Loading

2024/19/example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
r, wr, b, g, bwu, rb, gb, br
2+
3+
brwrr
4+
bggr
5+
gbbr
6+
rrbgbr
7+
ubwu
8+
bwurrg
9+
brgr
10+
bbrgwb

2024/19/script.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from GhostyUtils import aoc
2+
from functools import cache
3+
import re
4+
5+
6+
"""
7+
@cache
8+
def validate(design: str, patterns: frozenset[str]) -> bool:
9+
10+
if design in patterns:
11+
if aoc.args.verbose or aoc.args.progress:
12+
print(f"{design} - found")
13+
return True
14+
else:
15+
# try all splits
16+
valid = False
17+
for i in range(1, len(design)-1):
18+
valid = validate(design[:i], patterns) and validate(design[i:], patterns)
19+
if valid:
20+
break
21+
return valid
22+
23+
return False
24+
"""
25+
26+
27+
def validate(design: str, patterns: frozenset[str]) -> bool:
28+
pattern = re.compile(r"^(" + r'|'.join(patterns) + r")+$")
29+
return re.match(pattern, design)
30+
31+
32+
def main():
33+
patterns, designs = aoc.read_sections()
34+
patterns = frozenset(patterns.split(', '))
35+
designs = designs.splitlines()
36+
37+
valid = 0
38+
for design in designs:
39+
if validate(design, patterns):
40+
if aoc.args.verbose or aoc.args.progress:
41+
print(f"1 - {design}")
42+
valid += 1
43+
else:
44+
if aoc.args.verbose or aoc.args.progress:
45+
print(f"0 - {design}")
46+
47+
print(f"p1: {valid}")
48+
49+
50+
if __name__ == "__main__":
51+
main()

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
33

44
<!-- AOC TILES BEGIN -->
55
<h1 align="center">
6-
Advent of Code - 195/486
6+
Advent of Code - 196/488
77
</h1>
88
<h1 align="center">
9-
2024 - 34 ⭐ - Python
9+
2024 - 35 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@@ -62,6 +62,9 @@ My solutions to the yearly Advents of Code
6262
<a href="2024/18/script.py">
6363
<img src=".aoc_tiles/tiles/2024/18.png" width="161px">
6464
</a>
65+
<a href="2024/19/script.py">
66+
<img src=".aoc_tiles/tiles/2024/19.png" width="161px">
67+
</a>
6568
<h1 align="center">
6669
2023 - 47 ⭐ - Python
6770
</h1>

0 commit comments

Comments
 (0)