-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2022_day09.santa
74 lines (63 loc) · 1.01 KB
/
aoc2022_day09.santa
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
73
74
input: read("aoc://2022/9")
let signum = |x| match x {
0 { 0 }
n if n < 0 { -1 }
_ { 1 }
}
let parse_motions = lines >> flat_map |motion| {
let [direction, step] = split(" ", motion);
repeat(direction) |> take(int(step));
}
let directions = #{"U": [-1, 0], "D": [1, 0], "L": [0, -1], "R": [0, 1]};
let knot_head = scan([0, 0]) |point, direction| {
point `vec_add` directions[direction];
};
let knot_tail = scan([0, 0]) |[ty, tx], [hy, hx]| {
if abs(ty - hy) < 2 && abs(tx - hx) < 2 {
[ty, tx];
} else {
[ty + signum(hy - ty), tx + signum(hx - tx)]
}
}
let visits = set >> size;
part_one: {
parse_motions(input)
|> knot_head
|> knot_tail
|> visits;
}
part_two: {
parse_motions(input)
|> knot_head
|> iterate(knot_tail)
|> get(9)
|> visits;
}
test: {
input: "R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2"
part_one: 13
part_two: 1
}
test: {
input: "R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20"
part_two: 36
}
test: {
input: read("aoc://2022/9")
part_one: 5907
part_two: 2303
}