-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
50 lines (37 loc) · 1.15 KB
/
part2.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
from functools import cache
@cache
def count_arrangements(springs, groups):
springs = springs.lstrip('.')
if not springs:
return not groups
if not groups:
return all(spring != '#' for spring in springs)
if springs[0] == '#': # We are at the end of some group
group_size = groups[0]
if group_size > len(springs):
return 0
if any(springs[i] == '.' for i in range(group_size)):
return 0
if len(springs) > group_size and springs[group_size] == '#':
return 0
return count_arrangements(springs[group_size + 1:], groups[1:])
return count_arrangements('#' + springs[1:], groups) + count_arrangements(springs[1:], groups)
with open('input.txt') as file:
records = [
[springs, tuple(map(int, groups.split(',')))]
for springs, groups in (
line.split(' ', 1)
for line in filter(None, map(str.strip, file))
)
]
records = [
[
((row + '?') * 5)[:-1],
groups * 5
]
for row, groups in records
]
print(sum(
count_arrangements(springs, groups)
for springs, groups in records
))