-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.py
66 lines (45 loc) · 1.6 KB
/
day12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Advent of Code 2023 - Day 12."""
import functools
import itertools
import sys
from typing import TextIO
@functools.cache
def count_options(pattern: str, groups: tuple) -> int:
if len(pattern) == 0:
return 1 if len(groups) == 0 else 0
if pattern[0] == ".":
return count_options(pattern[1:], groups)
if pattern[0] == "?":
return count_options("." + pattern[1:], groups) + count_options(
"#" + pattern[1:], groups
)
# We found a broken spring
if len(groups) == 0:
return 0
want = groups[0]
have = 0
for _ in itertools.takewhile(lambda s: s in "#?", pattern):
have += 1
if have < want: # not enough broken springs
return 0
if pattern[want : want + 1] == "#": # too many broken springs
return 0
# Exactly enough broken springs. This means that the next character MUST be a ., even if it's a ?
return count_options(pattern[want + 1 :], groups[1:])
def solve_record(record: str, n: int) -> int:
pattern, groups = record.split()
return count_options(
"?".join([pattern] * n), tuple([*map(int, groups.split(","))] * n)
)
def part_one(file: TextIO) -> int:
return sum(solve_record(r, 1) for r in file)
def part_two(file: TextIO) -> int:
return sum(solve_record(r, 5) for r in file)
def main():
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file))
if __name__ == "__main__":
main()