-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2022_day13.santa
71 lines (54 loc) · 1.22 KB
/
aoc2022_day13.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
input: read("aoc://2022/13")
let parse_packets = lines >> filter(_ != "") >> map(evaluate);
let cmp = |a, b| if a == b { nil } else { a > b }
let compare = |l, r| match [type(l), type(r)] {
["List", "Integer"] { compare(l, [r]) }
["Integer", "List"] { compare([l], r) }
["Integer", "Integer"] { l `cmp` r }
["List", "List"] {
let check = zip(l, r) |> map(|[a, b]| compare(a, b)) |> find(_ != nil);
if check != nil { check } else { size(l) `cmp` size(r) }
}
}
part_one: {
let packet_pairs = parse_packets(input) |> chunk(2);
zip(1.., packet_pairs)
|> filter(|[idx, [l, r]]| !compare(l, r))
|> map(|[idx]| idx)
|> sum;
}
part_two: {
let divider_packets = [[[2]], [[6]]];
let all_packets = parse_packets(input) + divider_packets;
all_packets
|> sort(compare)
|> zip(1.., _)
|> filter(|[idx, packet]| divider_packets `includes?` packet)
|> map(|[idx]| idx)
|> reduce(*);
}
test: {
input: "[1,1,3,1,1]
[1,1,5,1,1]
[[1],[2,3,4]]
[[1],4]
[9]
[[8,7,6]]
[[4,4],4,4]
[[4,4],4,4,4]
[7,7,7,7]
[7,7,7]
[]
[3]
[[[]]]
[[]]
[1,[2,[3,[4,[5,6,7]]]],8,9]
[1,[2,[3,[4,[5,6,0]]]],8,9]"
part_one: 13
part_two: 140
}
test: {
input: read("aoc://2022/13")
part_one: 5503
part_two: 20952
}