-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.ndc
63 lines (57 loc) · 1.05 KB
/
12.ndc
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
init, lines := read_file("input/12").split_once("\n\n");
_, init := init.split_once(": ");
rules := %{:"."};
for line in lines.lines() {
from, to := line.split_once(" => ");
rules[from] = to;
}
fn evolve(cur,idx0) {
idx0 += 1;
// Pad with ...
cur := "..." ++ cur ++ "...";
len := cur.len();
new := "";
for i in 2..(len-2) {
slice := cur.slice(i-2,5);
new.append(rules[slice]);
}
ok := true;
trim := 0;
for c in new {
if c == "." and ok {
trim += 1;
} else {
ok = false;
}
}
l := new.len();
idx0 - trim, new.slice(trim, l - trim)
}
fn score(idx0, cur) {
idx := 0 - idx0;
ans := 0;
for c in cur {
if c == "#" {
ans += idx;
}
idx += 1;
}
ans
}
cur := init;
idx0 := 0;
print("0", cur);
i := 0;
a := [];
target := 50000000000;
while i < target {
idx0, cur = evolve(cur, idx0);
// print(i, idx0, score(idx0, cur), cur);
a.push(score(idx0, cur));
i += 1;
if a.len() >= 2 and a[-1] - a[-2] == 96 {
rem := target - i;
print(score(idx0 - rem, cur));
assert(false);
}
}