-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.zote
73 lines (59 loc) · 1.49 KB
/
12.zote
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
67
68
69
70
71
72
include!("stdlib");
include!("aoc.zote");
input := read("input");
lines := input >> split("\n");
cache := dict();
fn arrangements(springs, groups) -> {
dfs(springs, 0, groups, 0)
}
fn dfs(springs, ss, groups, gs) -> {
key := [ss, gs];
if key >> in(cache) return cache[key];
if gs == len(groups) {
for spring in springs[ss:] {
if spring == "#" return cache[key] = 0;
}
return cache[key] = 1;
}
ways := 0;
gsize := groups[gs];
while ss + gsize <= len(springs) {
// We can either take a step or use the group
// use the group
{
// See that the one after the group is not #, and that we don't use . as bad
if !(ss + gsize < len(springs) and springs[ss+gsize] == "#") and all(springs[ss:ss+gsize], \>> neq("."))
ways += dfs(springs, ss + gsize + 1, groups, gs + 1);
}
// Take another, and test again
{
if springs[ss] == '#' break;
ss += 1;
}
}
cache[key] = ways
}
// Part 1
{
tot := 0;
for line in lines {
(springs, groups) := line >> split(" ");
groups = groups >> split(",") >> map(int);
cache = dict();
tot += arrangements(springs, groups);
}
print(tot);
}
// Part 2
{
tot := 0;
for line in lines {
(springs, groups) := line >> split(" ");
springs = springs ++ "?" ++ springs ++ "?" ++ springs ++ "?" ++ springs ++ "?" ++ springs;
groups = groups ++ "," ++ groups ++ "," ++ groups ++ "," ++ groups ++ "," ++ groups;
groups = groups >> split(",") >> map(int);
cache = dict();
tot += arrangements(springs, groups);
}
print(tot);
}