-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.sql
41 lines (40 loc) · 880 Bytes
/
day3.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
40
41
-- part a
WITH data AS (
SELECT
arrayJoin(
extractAllGroupsVertical(line, 'mul\((\d{1,3}),(\d{1,3})\)')
) AS pairs
FROM
"table"
)
SELECT
sum(toInt64(pairs [1]) * toInt64(pairs [2]))
FROM
data;
-- part b
WITH data AS (
SELECT
extractAllGroupsVertical(
arrayFold(
(acc, curr) -> concat(acc, curr),
groupArray(line),
''
),
'(?:(do)\(\)|(don)\'t\(\)|mul\((\d{1,3}),(\d{1,3})\))\'?'
) AS pairs
FROM
"table"
)
SELECT
arrayFold(
(acc, curr) -> (
acc.1 + acc.2 * (
toInt64OrZero(curr [3]) * toInt64OrZero(curr [4])
),
multiIf(curr [1] = 'do', 1, curr [2] = 'don', 0, acc.2)
),
pairs,
(0 :: Int64, 1 :: Int64)
).1
FROM
data;