-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.sql
39 lines (38 loc) · 1004 Bytes
/
day2.sql
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
-- part a
SELECT
sum(
toInt64(splitByChar(' ', splitByChar(':', line) [1]) [2]) * arrayAll(
m -> m ['red'] <= 12
AND m ['green'] <= 13
AND m ['blue'] <= 14,
arrayMap(
x -> mapApply(
(k, v) -> (v, toInt64(k)),
extractKeyValuePairs(x, ' ', ',')
),
splitByString('; ', trim(splitByChar(':', line) [2]))
)
)
)
FROM
"table";
-- part b
SELECT
sum(
arrayProduct(
arrayFold((acc, m) -> [max2(acc[1], m ['red']),
max2(acc [2], m ['green']),
max2(acc [3], m ['blue']) ],
arrayMap(
x -> mapApply(
(k, v) -> (v, toInt64(k)),
extractKeyValuePairs(x, ' ', ',')
),
splitByString('; ', trim(splitByChar(':', line) [2]))
),
[0., 0., 0.]
)
)
)
FROM
"table";