-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_03_2.sql
41 lines (40 loc) · 1.56 KB
/
day_03_2.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
WITH RECURSIVE foo AS (
SELECT
1 AS pos,
(char = (CASE WHEN count_1 >= count_0 THEN '1' ELSE '0' END)) AS is_generator,
data
FROM (
SELECT
data,
substr(data, 1, 1) AS char,
count(*) FILTER (WHERE substr(data, 1, 1) = '0') OVER () AS count_0,
count(*) FILTER (WHERE substr(data, 1, 1) = '1') OVER () AS count_1
FROM aoc."2021_day_03"
) AS bar
WHERE (char = (CASE WHEN count_1 >= count_0 THEN '1' ELSE '0' END))
OR (char = (CASE WHEN count_0 <= count_1 THEN '0' ELSE '1' END))
UNION ALL
SELECT
pos + 1 AS pos,
is_generator,
data
FROM (
SELECT
pos,
is_generator,
data,
substr(data, pos+1, 1) AS char,
count(*) FILTER (WHERE substr(data, pos+1, 1) = '0') OVER (PARTITION BY pos, is_generator) AS count_0,
count(*) FILTER (WHERE substr(data, pos+1, 1) = '1') OVER (PARTITION BY pos, is_generator) AS count_1
FROM foo
) AS bar
WHERE (is_generator = TRUE AND (char = (CASE WHEN count_1 >= count_0 THEN '1' ELSE '0' END)))
OR (is_generator = FALSE AND (char = (CASE WHEN count_0 <= count_1 THEN '0' ELSE '1' END)))
)
SELECT max(data_int) * min(data_int) -- there is no multiply aggregate
FROM (
SELECT data::bit(12)::int
FROM foo
WHERE (is_generator = TRUE AND pos = ANY(SELECT max(pos) FROM foo WHERE is_generator = TRUE))
OR (is_generator = FALSE AND pos = ANY(SELECT max(pos) FROM foo WHERE is_generator = FALSE))
) AS src;