-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10-sqlite.sql
74 lines (74 loc) · 2.38 KB
/
day10-sqlite.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
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
WITH RECURSIVE "__1" ("addx", "ip", "line", "rest") AS (
SELECT
CAST(substr((CASE WHEN (instr(?1, '
') > 0) THEN substr(?1, 1, (instr(?1, '
') - 1)) ELSE ?1 END), 6) AS INTEGER) AS "addx",
(0 + 1) AS "ip",
(CASE WHEN (instr(?1, '
') > 0) THEN substr(?1, 1, (instr(?1, '
') - 1)) ELSE ?1 END) AS "line",
(CASE WHEN (instr(?1, '
') > 0) THEN substr(?1, (instr(?1, '
') + 1)) ELSE '' END) AS "rest"
WHERE (?1 <> '')
UNION ALL
SELECT
CAST(substr((CASE WHEN (instr("__2"."rest", '
') > 0) THEN substr("__2"."rest", 1, (instr("__2"."rest", '
') - 1)) ELSE "__2"."rest" END), 6) AS INTEGER) AS "addx",
("__2"."ip" + 1) AS "ip",
(CASE WHEN (instr("__2"."rest", '
') > 0) THEN substr("__2"."rest", 1, (instr("__2"."rest", '
') - 1)) ELSE "__2"."rest" END) AS "line",
(CASE WHEN (instr("__2"."rest", '
') > 0) THEN substr("__2"."rest", (instr("__2"."rest", '
') + 1)) ELSE '' END) AS "rest"
FROM "__1" AS "__2"
WHERE ("__2"."rest" <> '')
),
"cycles_1" ("x", "cycle") AS (
SELECT
(1 + (sum(("__3"."addx" * "values_2"."tick")) OVER (ORDER BY "__3"."ip", "values_2"."tick" ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING))) AS "x",
(1 + coalesce((count(*) OVER (ORDER BY "__3"."ip", "values_2"."tick" ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)), 0)) AS "cycle"
FROM "__1" AS "__3"
LEFT JOIN (
SELECT "values_1"."column1" AS "tick"
FROM (
VALUES
(0),
(1)
) AS "values_1"
) AS "values_2" ON ("__3"."line" <> 'noop')
)
SELECT
"cycles_3"."part1",
"cycles_8"."part2"
FROM (
SELECT sum(("cycles_2"."x" * "cycles_2"."cycle")) AS "part1"
FROM "cycles_1" AS "cycles_2"
WHERE (mod(("cycles_2"."cycle" + 20), 40) = 0)
) AS "cycles_3"
CROSS JOIN (
SELECT group_concat("cycles_7"."line", '
') AS "part2"
FROM (
SELECT group_concat("cycles_6"."pixel", '') AS "line"
FROM (
SELECT
"cycles_5"."row",
(CASE WHEN ("cycles_5"."col" BETWEEN "cycles_5"."x" AND ("cycles_5"."x" + 2)) THEN '#' ELSE '.' END) AS "pixel"
FROM (
SELECT
(1 + (("cycles_4"."cycle" - 1) / 40)) AS "row",
(1 + mod(("cycles_4"."cycle" - 1), 40)) AS "col",
"cycles_4"."x"
FROM "cycles_1" AS "cycles_4"
) AS "cycles_5"
ORDER BY
"cycles_5"."row",
"cycles_5"."col"
) AS "cycles_6"
GROUP BY "cycles_6"."row"
ORDER BY "cycles_6"."row"
) AS "cycles_7"
) AS "cycles_8"