-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
50 lines (37 loc) · 1.28 KB
/
solver.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
import functools
import inspect
import os
cwd = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
with open(os.path.join(cwd, "input.txt"), "r") as f:
entries = [
(record, tuple(map(int, groups.split(","))))
for record, groups in [entry.split() for entry in f.read().splitlines()]
]
@functools.cache
def count_permutations(record, groups, count=0):
if not record:
# if all groups are matched, return 1
return len(groups) == 0
res = 0
branches = record[0].replace("?", ".#")
for branch in branches:
if branch == "#":
# we either start a new group or continue a group
res += count_permutations(record[1:], groups, count + 1)
elif count:
# we just ended a group, check if it matches the group we are on
if groups and groups[0] == count:
res += count_permutations(record[1:], groups[1:])
else:
# we are outside a group, keep going
res += count_permutations(record[1:], groups)
return res
# part 1
print(sum(count_permutations(f"{record}.", groups) for record, groups in entries))
# part 2
print(
sum(
count_permutations(f"{'?'.join([record] * 5)}.", groups * 5)
for record, groups in entries
)
)