-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.zote
61 lines (48 loc) · 1.25 KB
/
19.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
include!("stdlib");
include!("aoc.zote");
input := read("input") >> trim;
(rules, parts) := input >> split('\n\n') >> map(\>> split("\n"));
// Parse the input
rdict := dict();
for rule in rules {
(name, rest) := rule >> split('{');
rest := rest[:-1];
parts := rest >> split(",");
paths := [];
for part in parts[:-1] {
(r, to) := part >> split(':');
[part[0], if part[1] == '>' gt else lt, int(r[2:]), to] >> push(paths);
}
rdict[name] = [paths, parts[-1]];
}
// Part 1
fn valid(part, wf) -> {
if wf == "A" return sum(part >> values) else if wf == "R" return 0;
(paths, def) := rdict[wf];
for (t, op, cmp, to) in paths {
if part[t] >> op(cmp) return valid(part, to);
}
valid(part, def)
}
parts
>> map(\>> ints >> zip('xmas') >> map(rev) >> dict >> valid('in'))
>> sum
>> print;
// Part 2
fn possibilities(poss, wf) -> {
if wf == "A" return poss >> values >> map(len) >> prod else if wf == "R" return 0;
tot := 0;
(paths, def) := rdict[wf];
for (t, op, cmp, to) in paths {
p2 := poss >> clone;
p2[t] = p2[t] >> filter(\>> op(cmp));
poss[t] = poss[t] >> filter(\>> op(cmp) >> not);
tot += possibilities(p2, to);
}
tot + possibilities(poss, def)
}
'xmas'
>> map(\c -> [c, [1:4001]])
>> dict
>> possibilities('in')
>> print;