-
Notifications
You must be signed in to change notification settings - Fork 5
/
p9.noul
61 lines (50 loc) · 1.11 KB
/
p9.noul
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
day := 9;
import "advent-prelude.noul";
puzzle_input := advent_input();
directions := {
"U": V(-1,0),
"R": V(0,1),
"D": V(1,0),
"L": V(0,-1),
};
# Part 1
h := V(0, 0);
t := V(0, 0);
seen := {t};
for (line <- puzzle_input.lines; dir, cnt' := words(line); cnt := int(cnt'); _ <- 1 to cnt) (
h += directions[dir];
td := switch (t - h)
case 2, _ -> V( 1, 0)
case -2, _ -> V(-1, 0)
case _, 2 -> V( 0, 1)
case _, -2 -> V( 0, -1)
case x -> x;
t = h + td;
seen |.= t;
);
submit! 1, len(seen);
# Part 2
h = V(0, 0);
ts := V(0, 0) .* 9;
seen = {ts[0]};
for (line <- puzzle_input.lines; dir, cnt' := words(line); cnt := int(cnt'); _ <- 1 to cnt) (
h += directions[dir];
prev := h;
for (ti <- 0 to 8) (
td := switch (ts[ti] - prev)
case 2, 2 -> V( 1, 1)
case 2, -2 -> V( 1, -1)
case -2, 2 -> V(-1, 1)
case -2, -2 -> V(-1, -1)
case 2, _ -> V( 1, 0)
case -2, _ -> V(-1, 0)
case _, 2 -> V( 0, 1)
case _, -2 -> V( 0, -1)
case x -> x;
ts[ti] = prev + td;
prev = ts[ti];
);
seen |.= last ts;
);
submit! 2, len(seen);
# this could look much simpler; see p9-post.noul