-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday_10_1.sql
More file actions
30 lines (28 loc) · 831 Bytes
/
day_10_1.sql
File metadata and controls
30 lines (28 loc) · 831 Bytes
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
WITH RECURSIVE line AS (
SELECT
src.line_id,
1 AS step,
src.original_line,
regexp_replace(original_line, '\[\]|\<\>|\(\)|\{\}', '', 'g') AS current_line
FROM aoc."2021_day_10" AS src(line_id, original_line)
UNION ALL
SELECT
prev_line.line_id,
step + 1 AS step,
original_line,
replaced AS current_line
FROM line AS prev_line
CROSS JOIN LATERAL (SELECT regexp_replace(current_line, '\[\]|\<\>|\(\)|\{\}', '', 'g')) AS _(replaced)
WHERE replaced != current_line
)
SELECT sum(CASE c
WHEN ')' THEN 3
WHEN ']' THEN 57
WHEN '}' THEN 1197
WHEN '>' THEN 25137
END)
FROM (
SELECT substr(ltrim((array_agg(current_line ORDER BY step DESC))[1], '<[{('), 1, 1) AS c
FROM line
GROUP BY line_id
) AS src;