-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.zote
46 lines (36 loc) · 855 Bytes
/
15.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
include!("stdlib");
include!("aoc.zote");
input := read("input");
line := input >> split("\n") >> [0];
fn hash(str) -> {
res := 0;
for char in str {
res += to_ascii(char);
res *= 17;
res = res % 256;
}
res
}
line >> split(",") >> map(hash) >> sum >> print;
boxes := [0:256] >> map(\>> const([]));
fn take_step(step) -> {
if step[-1] != '-' {
(name, foc) := step >> split("=");
h := hash(name);
box := boxes[h];
for (i, len) in box >> enumerate if len[0] == name {
len[1] = int(foc);
return;
}
[name, int(foc)] >> push(box);
} else {
name := step[:-1];
h := hash(name);
boxes[h] = boxes[h] >> filter(\len -> len[0] != name);
}
}
for step in line >> split(",") take_step(step);
res := 0;
for (boxi, box) in boxes >> enumerate for (i, (name, foc)) in box >> enumerate
res += (i+1)*foc*(boxi+1);
print(res);