-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09_02.sql
312 lines (293 loc) · 13 KB
/
09_02.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
create table T (row text) strict;
.import 09_input.txt T
.load ./regex0.so
.mode box
-- parse input
with
-- M is the parsed map
--
-- x is the column index
-- y is the row index
-- v is the char at (x, y)
M(c, v) as materialized (
select
R.start,
R.match
from regex_find_all(".", T.row) as R
join T
),
E(c, start, end, type) as materialized (
select
c,
lag (end, 1, 0) over (order by c) as start,
end,
case c % 2 when 0 then 'data' else 'empty' end as type
from (
select c, sum(v) over (order by c) as end from M
)
),
-- space blocks
S(sc, start, end, size, type) as (
select (c-1)/2, start, end, end - start, type from E where type == 'empty'
),
-- data blocks
D(dc, start, end, size, type) as (
select c/2, start, end, end - start, type from E where type == 'data' order by c desc
),
-- data blocks divided by sizes
D9(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 9),
D8(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 8),
D7(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 7),
D6(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 6),
D5(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 5),
D4(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 4),
D3(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 3),
D2(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 2),
D1(idx, dc) as (select row_number() over (order by dc desc)-1, dc from D where size = 1),
-- To assign the data to free slots we need to create a recursive CTE
-- where all branches are mutually exclusive. This is kind of messy but
-- I don't really see any other solution.
--
-- At least this gives us a linear time algorithm.
--
-- space_left is the number of unclaimed bits in the current space block
-- offset is the number of claimed bits in the current space block
-- sc is the index of the current space block
-- dc is the index of the picked data block
-- p9, p8, ..., p1 are the number of picked data blocks of each size
X(space_left, offset, sc, dc, p9, p8, p7, p6, p5, p4, p3, p2, p1) as (
select size, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0 from S where sc = 0
-- case: pick the next data block of size 1
union all
select space_left-1, offset+1, sc, D1.dc, p9, p8, p7, p6, p5, p4, p3, p2, p1+1 as c from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is null or D1.dc > D9.dc) and
(D8.dc is null or D1.dc > D8.dc) and
(D7.dc is null or D1.dc > D7.dc) and
(D6.dc is null or D1.dc > D6.dc) and
(D5.dc is null or D1.dc > D5.dc) and
(D4.dc is null or D1.dc > D4.dc) and
(D3.dc is null or D1.dc > D3.dc) and
(D2.dc is null or D1.dc > D2.dc) and
(D1.dc is not null)
-- case: pick the next data block of size 2
union all
select space_left-2, offset+2, sc, D2.dc, p9, p8, p7, p6, p5, p4, p3, p2+1, p1 from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is null or D2.dc > D9.dc) and
(D8.dc is null or D2.dc > D8.dc) and
(D7.dc is null or D2.dc > D7.dc) and
(D6.dc is null or D2.dc > D6.dc) and
(D5.dc is null or D2.dc > D5.dc) and
(D4.dc is null or D2.dc > D4.dc) and
(D3.dc is null or D2.dc > D3.dc) and
(D2.dc is not null) and
(D1.dc is null or D2.dc > D1.dc)
-- case: pick the next data block of size 3
union all
select space_left-3, offset+3, sc, D3.dc as c, p9, p8, p7, p6, p5, p4, p3+1, p2, p1 from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is null or D3.dc > D9.dc) and
(D8.dc is null or D3.dc > D8.dc) and
(D7.dc is null or D3.dc > D7.dc) and
(D6.dc is null or D3.dc > D6.dc) and
(D5.dc is null or D3.dc > D5.dc) and
(D4.dc is null or D3.dc > D4.dc) and
(D3.dc is not null) and
(D2.dc is null or D3.dc > D2.dc) and
(D1.dc is null or D3.dc > D1.dc)
-- case: pick the next data block of size 4
union all
select space_left-4, offset+4, sc, D4.dc as c, p9, p8, p7, p6, p5, p4+1, p3, p2, p1 from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is null or D4.dc > D9.dc) and
(D8.dc is null or D4.dc > D8.dc) and
(D7.dc is null or D4.dc > D7.dc) and
(D6.dc is null or D4.dc > D6.dc) and
(D5.dc is null or D4.dc > D5.dc) and
(D4.dc is not null) and
(D3.dc is null or D4.dc > D3.dc) and
(D2.dc is null or D4.dc > D2.dc) and
(D1.dc is null or D4.dc > D1.dc)
-- case: pick the next data block of size 5
union all
select space_left-5, offset+5, sc, D5.dc as c, p9, p8, p7, p6, p5+1, p4, p3, p2, p1 from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is null or D5.dc > D9.dc) and
(D8.dc is null or D5.dc > D8.dc) and
(D7.dc is null or D5.dc > D7.dc) and
(D6.dc is null or D5.dc > D6.dc) and
(D5.dc is not null) and
(D4.dc is null or D5.dc > D4.dc) and
(D3.dc is null or D5.dc > D3.dc) and
(D2.dc is null or D5.dc > D2.dc) and
(D1.dc is null or D5.dc > D1.dc)
-- case: pick the next data block of size 6
union all
select space_left-6, offset+6, sc, D6.dc as c, p9, p8, p7, p6+1, p5, p4, p3, p2, p1 from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is null or D6.dc > D9.dc) and
(D8.dc is null or D6.dc > D8.dc) and
(D7.dc is null or D6.dc > D7.dc) and
(D6.dc is not null) and
(D5.dc is null or D6.dc > D5.dc) and
(D4.dc is null or D6.dc > D4.dc) and
(D3.dc is null or D6.dc > D3.dc) and
(D2.dc is null or D6.dc > D2.dc) and
(D1.dc is null or D6.dc > D1.dc)
-- case: pick the next data block of size 7
union all
select space_left-7, offset+7, sc, D7.dc as c, p9, p8, p7+1, p6, p5, p4, p3, p2, p1 from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is null or D7.dc > D9.dc) and
(D8.dc is null or D7.dc > D8.dc) and
(D7.dc is not null) and
(D6.dc is null or D7.dc > D6.dc) and
(D5.dc is null or D7.dc > D5.dc) and
(D4.dc is null or D7.dc > D4.dc) and
(D3.dc is null or D7.dc > D3.dc) and
(D2.dc is null or D7.dc > D2.dc) and
(D1.dc is null or D7.dc > D1.dc)
-- case: pick the next data block of size 8
union all
select space_left-8, offset+8, sc, D8.dc as c, p9, p8+1, p7, p6, p5, p4, p3, p2, p1 from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is null or D8.dc > D9.dc) and
(D8.dc is not null) and
(D7.dc is null or D8.dc > D7.dc) and
(D6.dc is null or D8.dc > D6.dc) and
(D5.dc is null or D8.dc > D5.dc) and
(D4.dc is null or D8.dc > D4.dc) and
(D3.dc is null or D8.dc > D3.dc) and
(D2.dc is null or D8.dc > D2.dc) and
(D1.dc is null or D8.dc > D1.dc)
-- case: pick the next data block of size 9
union all
select space_left-9, offset+9, sc, D9.dc as c, p9+1, p8, p7, p6, p5, p4, p3, p2, p1 from X
left join D9 on D9.dc > sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > sc and D1.idx = X.p1 and space_left >= 1
where
(D9.dc is not null) and
(D8.dc is null or D9.dc > D8.dc) and
(D7.dc is null or D9.dc > D7.dc) and
(D6.dc is null or D9.dc > D6.dc) and
(D5.dc is null or D9.dc > D5.dc) and
(D4.dc is null or D9.dc > D4.dc) and
(D3.dc is null or D9.dc > D3.dc) and
(D2.dc is null or D9.dc > D2.dc) and
(D1.dc is null or D9.dc > D1.dc)
-- case: move on to the next space block
union all
select S.size, 0, S.sc, NULL, p9, p8, p7, p6, p5, p4, p3, p2, p1 from X
left join D9 on D9.dc > X.sc and D9.idx = X.p9 and space_left >= 9
left join D8 on D8.dc > X.sc and D8.idx = X.p8 and space_left >= 8
left join D7 on D7.dc > X.sc and D7.idx = X.p7 and space_left >= 7
left join D6 on D6.dc > X.sc and D6.idx = X.p6 and space_left >= 6
left join D5 on D5.dc > X.sc and D5.idx = X.p5 and space_left >= 5
left join D4 on D4.dc > X.sc and D4.idx = X.p4 and space_left >= 4
left join D3 on D3.dc > X.sc and D3.idx = X.p3 and space_left >= 3
left join D2 on D2.dc > X.sc and D2.idx = X.p2 and space_left >= 2
left join D1 on D1.dc > X.sc and D1.idx = X.p1 and space_left >= 1
join S where
(S.sc = X.sc + 1) and
(D9.dc is null) and
(D8.dc is null) and
(D7.dc is null) and
(D6.dc is null) and
(D5.dc is null) and
(D4.dc is null) and
(D3.dc is null) and
(D2.dc is null) and
(D1.dc is null)
),
F as (
select
D.dc,
coalesce(S.start + X.offset - D.size, D.start) as start,
coalesce(S.start + X.offset, D.end) as end
from D
left join X on D.dc = X.dc
left join S on S.sc = X.sc
order by start
)
select sum(value * dc) from F
join generate_series(F.start, F.end-1);