aoc_1204.slt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. # https://github.com/MaterializeInc/advent-of-code-2023/blob/main/week1/aoc_1204.md
  10. mode cockroach
  11. statement ok
  12. CREATE TABLE aoc_1204 (input TEXT);
  13. statement ok
  14. INSERT INTO aoc_1204 VALUES (
  15. 'Card 1: 91 58 89 8 19 64 92 28 22 1 | 6 94 21 70 81 59 5 35 24 31 43 69 91 12 51 53 98 50 70 98 47 6 9 49 50
  16. Card 2: 49 56 57 80 28 9 3 19 55 6 | 35 25 76 45 35 73 12 93 29 23 50 33 75 36 4 33 90 84 1 9 44 62 99 80 85
  17. Card 3: 97 29 93 95 66 40 97 9 58 11 | 22 56 90 13 40 84 83 70 65 80 73 84 58 93 98 79 46 51 47 70 8 50 43 70
  18. Card 4: 62 79 90 45 63 70 75 26 14 92 | 70 5 69 58 80 64 72 4 36 24 40 76 79 16 79 11 80 88 49 92 15 24 5 49 22
  19. Card 5: 54 26 80 65 14 46 77 59 12 20 | 96 89 95 25 19 22 34 9 24 86 87 63 16 31 5 22 91 71 8 80 33 2 65 67 78
  20. Card 6: 22 10 58 44 5 97 97 57 88 8 | 54 50 79 45 2 40 90 30 82 37 29 99 50 90 51 84 97 62 8 4 89 82 86 59 65
  21. Card 7: 65 94 76 4 41 40 1 6 50 96 | 82 90 42 92 22 18 29 96 47 91 71 2 5 3 42 73 45 26 15 13 29 37 7 63 81
  22. Card 8: 73 19 52 43 47 54 6 86 12 34 | 25 70 26 15 35 10 65 81 48 72 98 48 18 94 8 34 6 44 79 25 77 27 78 61 28
  23. Card 9: 32 51 38 86 17 56 42 4 67 38 | 55 5 26 91 98 11 52 1 48 13 55 95 60 15 16 51 54 22 91 8 26 70 26 35 92
  24. Card 10: 6 40 74 5 31 63 1 5 12 64 | 88 7 91 54 4 62 37 66 5 69 59 78 17 47 61 2 6 56 36 59 2 71 63 87 72');
  25. statement ok
  26. CREATE VIEW input (input) AS SELECT * FROM aoc_1204;
  27. query I
  28. WITH parsed AS (
  29. SELECT regexp_split_to_table(input, '\n') AS line FROM aoc_1204
  30. ),
  31. numbers AS (
  32. SELECT split_part(line,':',1) AS card_id,
  33. replace(split_part(line,':',2),'|','') AS nrs
  34. FROM parsed
  35. ),
  36. arr AS (
  37. SELECT card_id,
  38. nrs,
  39. regexp_split_to_array(ltrim(rtrim(nrs)),'\s') AS nrs_arr
  40. FROM numbers
  41. ),
  42. winning AS (
  43. SELECT card_id,
  44. unnest(array_remove(nrs_arr,'')) nr,
  45. ROW_NUMBER() OVER (PARTITION BY card_id) AS row_num
  46. FROM arr
  47. GROUP BY card_id, nr HAVING COUNT(*)>1
  48. ORDER BY card_id
  49. ),
  50. winning_points AS (
  51. SELECT ROUND(EXP(SUM(LN(CASE WHEN row_num = 1 THEN row_num ELSE 2 END)))) AS points
  52. FROM winning
  53. GROUP BY card_id
  54. )
  55. SELECT SUM(points)
  56. FROM winning_points;
  57. ----
  58. 184
  59. query T multiline
  60. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  61. WITH parsed AS (
  62. SELECT regexp_split_to_table(input, '\n') AS line FROM aoc_1204
  63. ),
  64. numbers AS (
  65. SELECT split_part(line,':',1) AS card_id,
  66. replace(split_part(line,':',2),'|','') AS nrs
  67. FROM parsed
  68. ),
  69. arr AS (
  70. SELECT card_id,
  71. nrs,
  72. regexp_split_to_array(ltrim(rtrim(nrs)),'\s') AS nrs_arr
  73. FROM numbers
  74. ),
  75. winning AS (
  76. SELECT card_id,
  77. unnest(array_remove(nrs_arr,'')) nr,
  78. ROW_NUMBER() OVER (PARTITION BY card_id) AS row_num
  79. FROM arr
  80. GROUP BY card_id, nr HAVING COUNT(*)>1
  81. ORDER BY card_id
  82. ),
  83. winning_points AS (
  84. SELECT ROUND(EXP(SUM(LN(CASE WHEN row_num = 1 THEN row_num ELSE 2 END)))) AS points
  85. FROM winning
  86. GROUP BY card_id
  87. )
  88. SELECT SUM(points)
  89. FROM winning_points;
  90. ----
  91. Explained Query:
  92. With
  93. cte l0 =
  94. Reduce aggregates=[sum(roundf64(expf64(#0{sum})))] // { arity: 1 }
  95. Project (#1{sum}) // { arity: 1 }
  96. Reduce group_by=[record_get[0](record_get[1](#0))] aggregates=[sum(lnf64(bigint_to_double(case when (1 = record_get[0](#0)) then record_get[0](#0) else 2 end)))] // { arity: 2 }
  97. Project (#1) // { arity: 1 }
  98. FlatMap unnest_list(#0{row_number}) // { arity: 2 }
  99. Project (#1{row_number}) // { arity: 1 }
  100. Reduce group_by=[#0] aggregates=[row_number[order_by=[]](row(list[row(#0, #1, #2{count})]))] // { arity: 2 }
  101. Filter (#2{count} > 1) // { arity: 3 }
  102. Reduce group_by=[#0, #1] aggregates=[count(*)] // { arity: 3 }
  103. Project (#0, #2) // { arity: 2 }
  104. FlatMap unnest_array(array_remove(#1{nrs_arr}, "")) // { arity: 3 }
  105. Project (#2, #3) // { arity: 2 }
  106. Map (split_string(#1{line}, ":", 1), regexp_split_to_array["\s", case_insensitive=false](ltrim(rtrim(replace(split_string(#1{line}, ":", 2), "|", ""))))) // { arity: 4 }
  107. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](#0{input})) // { arity: 2 }
  108. ReadStorage materialize.public.aoc_1204 // { arity: 1 }
  109. Return // { arity: 1 }
  110. Union // { arity: 1 }
  111. Get l0 // { arity: 1 }
  112. Map (null) // { arity: 1 }
  113. Union // { arity: 0 }
  114. Negate // { arity: 0 }
  115. Project () // { arity: 0 }
  116. Get l0 // { arity: 1 }
  117. Constant // { arity: 0 }
  118. - ()
  119. Source materialize.public.aoc_1204
  120. Target cluster: quickstart
  121. EOF
  122. query I
  123. WITH MUTUALLY RECURSIVE
  124. lines(line string) AS (
  125. SELECT
  126. regexp_split_to_table(input, '\n') AS line
  127. FROM
  128. aoc_1204
  129. ),
  130. cards(match string[]) AS (
  131. SELECT
  132. regexp_match(line, 'Card +(\d+): (.*)') AS match
  133. FROM
  134. lines
  135. ),
  136. card_parts(card_id int, parts string[]) AS (
  137. SELECT
  138. match[1]::int AS card_id,
  139. regexp_split_to_array(match[2], ' \| ') AS parts
  140. FROM
  141. cards
  142. ),
  143. winners(card_id int, val int) AS (
  144. SELECT
  145. card_id,
  146. regexp_split_to_table(trim(parts[1]), '\s+')::int AS val
  147. FROM
  148. card_parts
  149. ),
  150. ours(card_id int, val int) AS (
  151. SELECT
  152. card_id,
  153. regexp_split_to_table(trim(parts[2]), '\s+')::int AS val
  154. FROM
  155. card_parts
  156. ),
  157. count_winning_numbers(card_id int, count int) AS (
  158. SELECT
  159. ours.card_id,
  160. count(winners.val)::int AS count
  161. FROM
  162. ours LEFT OUTER JOIN winners ON (
  163. ours.card_id = winners.card_id AND
  164. ours.val = winners.val
  165. )
  166. GROUP BY ours.card_id
  167. ),
  168. prizes(card_id int, prize_id int) AS (
  169. SELECT
  170. card_id,
  171. prize_id
  172. FROM
  173. count_winning_numbers CROSS JOIN generate_series(card_id + 1, card_id + count) AS prize_id
  174. UNION
  175. SELECT
  176. 0 AS card_id,
  177. ours.card_id AS prize_id
  178. FROM
  179. ours
  180. ),
  181. multipliers(card_id int, multiplier int) AS (
  182. SELECT
  183. prizes.prize_id AS card_id,
  184. SUM(coalesce(multipliers.multiplier, 1))::int AS multiplier
  185. FROM
  186. prizes left outer JOIN multipliers ON (
  187. prizes.card_id = multipliers.card_id
  188. )
  189. GROUP BY prizes.prize_id
  190. )
  191. SELECT
  192. SUM(multiplier) AS answer
  193. FROM
  194. multipliers;
  195. ----
  196. 978
  197. query T multiline
  198. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  199. WITH MUTUALLY RECURSIVE
  200. lines(line string) AS (
  201. SELECT
  202. regexp_split_to_table(input, '\n') AS line
  203. FROM
  204. aoc_1204
  205. ),
  206. cards(match string[]) AS (
  207. SELECT
  208. regexp_match(line, 'Card +(\d+): (.*)') AS match
  209. FROM
  210. lines
  211. ),
  212. card_parts(card_id int, parts string[]) AS (
  213. SELECT
  214. match[1]::int AS card_id,
  215. regexp_split_to_array(match[2], ' \| ') AS parts
  216. FROM
  217. cards
  218. ),
  219. winners(card_id int, val int) AS (
  220. SELECT
  221. card_id,
  222. regexp_split_to_table(trim(parts[1]), '\s+')::int AS val
  223. FROM
  224. card_parts
  225. ),
  226. ours(card_id int, val int) AS (
  227. SELECT
  228. card_id,
  229. regexp_split_to_table(trim(parts[2]), '\s+')::int AS val
  230. FROM
  231. card_parts
  232. ),
  233. count_winning_numbers(card_id int, count int) AS (
  234. SELECT
  235. ours.card_id,
  236. count(winners.val)::int AS count
  237. FROM
  238. ours LEFT OUTER JOIN winners ON (
  239. ours.card_id = winners.card_id AND
  240. ours.val = winners.val
  241. )
  242. GROUP BY ours.card_id
  243. ),
  244. prizes(card_id int, prize_id int) AS (
  245. SELECT
  246. card_id,
  247. prize_id
  248. FROM
  249. count_winning_numbers CROSS JOIN generate_series(card_id + 1, card_id + count) AS prize_id
  250. UNION
  251. SELECT
  252. 0 AS card_id,
  253. ours.card_id AS prize_id
  254. FROM
  255. ours
  256. ),
  257. multipliers(card_id int, multiplier int) AS (
  258. SELECT
  259. prizes.prize_id AS card_id,
  260. SUM(coalesce(multipliers.multiplier, 1))::int AS multiplier
  261. FROM
  262. prizes left outer JOIN multipliers ON (
  263. prizes.card_id = multipliers.card_id
  264. )
  265. GROUP BY prizes.prize_id
  266. )
  267. SELECT
  268. SUM(multiplier) AS answer
  269. FROM
  270. multipliers;
  271. ----
  272. Explained Query:
  273. With
  274. cte l0 =
  275. Project (#3, #4) // { arity: 2 }
  276. Map (regexp_match["Card +(\d+): (.*)", case_insensitive=false](#1{line}), text_to_integer(array_index(#2{match}, 1)), regexp_split_to_array[" \| ", case_insensitive=false](array_index(#2{match}, 2))) // { arity: 5 }
  277. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](#0{input})) // { arity: 2 }
  278. ReadStorage materialize.public.aoc_1204 // { arity: 1 }
  279. cte l1 =
  280. Project (#0, #3) // { arity: 2 }
  281. Map (text_to_integer(#2{unnest})) // { arity: 4 }
  282. FlatMap unnest_array(regexp_split_to_array["\s+", case_insensitive=false](btrim(array_index(#1{parts}, 2)))) // { arity: 3 }
  283. Get l0 // { arity: 2 }
  284. cte l2 =
  285. ArrangeBy keys=[[#0{card_id}, #1{val}]] // { arity: 2 }
  286. Filter (#0{card_id}) IS NOT NULL AND (#1{val}) IS NOT NULL // { arity: 2 }
  287. Get l1 // { arity: 2 }
  288. cte l3 =
  289. Project (#0, #1) // { arity: 2 }
  290. Join on=(#0{card_id} = #2{card_id} AND #1{val} = #3{val}) type=differential // { arity: 4 }
  291. implementation
  292. %0:l2[#0{card_id}, #1{val}]KK » %1[#0{card_id}, #1{val}]KK
  293. Get l2 // { arity: 2 }
  294. ArrangeBy keys=[[#0{card_id}, #1{val}]] // { arity: 2 }
  295. Project (#0, #3) // { arity: 2 }
  296. Filter (#2{unnest}) IS NOT NULL // { arity: 4 }
  297. Map (text_to_integer(#2{unnest})) // { arity: 4 }
  298. FlatMap unnest_array(regexp_split_to_array["\s+", case_insensitive=false](btrim(array_index(#1{parts}, 1)))) // { arity: 3 }
  299. Filter (#0{card_id}) IS NOT NULL // { arity: 2 }
  300. Get l0 // { arity: 2 }
  301. cte l4 =
  302. Distinct project=[#0, #1] // { arity: 2 }
  303. Union // { arity: 2 }
  304. Project (#0, #2) // { arity: 2 }
  305. FlatMap generate_series((#0{card_id} + 1), (#0{card_id} + #1{count}), 1) // { arity: 3 }
  306. Project (#0, #2) // { arity: 2 }
  307. Map (bigint_to_integer(#1{count})) // { arity: 3 }
  308. Reduce group_by=[#0] aggregates=[count(#1{val})] // { arity: 2 }
  309. Union // { arity: 2 }
  310. Map (null) // { arity: 2 }
  311. Union // { arity: 1 }
  312. Negate // { arity: 1 }
  313. Project (#0) // { arity: 1 }
  314. Join on=(#0{card_id} = #2 AND #1{val} = #3) type=differential // { arity: 4 }
  315. implementation
  316. %1[#0, #1]UKKA » %0:l2[#0{card_id}, #1{val}]KK
  317. Get l2 // { arity: 2 }
  318. ArrangeBy keys=[[#0, #1]] // { arity: 2 }
  319. Distinct project=[#0, #1] // { arity: 2 }
  320. Get l3 // { arity: 2 }
  321. Project (#0) // { arity: 1 }
  322. Get l1 // { arity: 2 }
  323. Get l3 // { arity: 2 }
  324. Project (#2, #0) // { arity: 2 }
  325. Map (0) // { arity: 3 }
  326. Get l1 // { arity: 2 }
  327. Return // { arity: 1 }
  328. With Mutually Recursive
  329. cte l5 =
  330. Project (#1, #3) // { arity: 2 }
  331. Join on=(#0{card_id} = #2) type=differential // { arity: 4 }
  332. implementation
  333. %1:l6[#0]UK » %0:l4[#0{card_id}]K
  334. ArrangeBy keys=[[#0{card_id}]] // { arity: 2 }
  335. Filter (#0{card_id}) IS NOT NULL // { arity: 2 }
  336. Get l4 // { arity: 2 }
  337. ArrangeBy keys=[[#0]] // { arity: 2 }
  338. Filter (#0{card_id}) IS NOT NULL // { arity: 2 }
  339. Get l6 // { arity: 2 }
  340. cte l6 =
  341. Project (#0, #2) // { arity: 2 }
  342. Map (bigint_to_integer(#1{sum})) // { arity: 3 }
  343. Reduce group_by=[#0] aggregates=[sum(coalesce(#1{multiplier}, 1))] // { arity: 2 }
  344. Union // { arity: 2 }
  345. Map (null) // { arity: 2 }
  346. Union // { arity: 1 }
  347. Negate // { arity: 1 }
  348. Project (#0) // { arity: 1 }
  349. Get l5 // { arity: 2 }
  350. Project (#1) // { arity: 1 }
  351. Get l4 // { arity: 2 }
  352. Get l5 // { arity: 2 }
  353. Return // { arity: 1 }
  354. With
  355. cte l7 =
  356. Reduce aggregates=[sum(#0{multiplier})] // { arity: 1 }
  357. Project (#1) // { arity: 1 }
  358. Get l6 // { arity: 2 }
  359. Return // { arity: 1 }
  360. Union // { arity: 1 }
  361. Get l7 // { arity: 1 }
  362. Map (null) // { arity: 1 }
  363. Union // { arity: 0 }
  364. Negate // { arity: 0 }
  365. Project () // { arity: 0 }
  366. Get l7 // { arity: 1 }
  367. Constant // { arity: 0 }
  368. - ()
  369. Source materialize.public.aoc_1204
  370. Target cluster: quickstart
  371. EOF
  372. query II
  373. WITH MUTUALLY RECURSIVE
  374. -- PART 0
  375. -- Parse the input as lines of text with line numbers.
  376. lines(line TEXT) AS (
  377. SELECT regexp_split_to_table(input, '\n')
  378. FROM input
  379. ),
  380. blocks(card TEXT, wins TEXT, have TEXT) AS (
  381. SELECT
  382. TRIM (regexp_split_to_array(line, '(:|\|)')[1]),
  383. TRIM (regexp_split_to_array(line, '(:|\|)')[2]),
  384. TRIM (regexp_split_to_array(line, '(:|\|)')[3])
  385. FROM
  386. lines
  387. ),
  388. parsed(card INT, wins TEXT[], have TEXT[]) AS (
  389. SELECT
  390. regexp_match(card, '[0-9]+')[1]::INT,
  391. regexp_split_to_array(wins, ' '),
  392. regexp_split_to_array(have, ' ')
  393. FROM blocks
  394. ),
  395. -- PART 1
  396. -- Count "have"s in "wins" for each row, exponentiate, sum.
  397. matches(card INT, score BIGINT) AS (
  398. SELECT card, (
  399. SELECT COUNT(*)
  400. FROM (
  401. SELECT unnest(wins) w
  402. INTERSECT
  403. SELECT unnest(have) w
  404. )
  405. WHERE w != ''
  406. )
  407. FROM parsed
  408. ),
  409. part1(part1 NUMERIC) AS (
  410. SELECT SUM(pow(2, score - 1))::NUMERIC
  411. FROM matches
  412. WHERE score > 0
  413. ),
  414. -- PART 2
  415. -- Each card provides a copy of the next `score` cards.
  416. -- This could be prefix sum if we want to be clever ...
  417. expanded(card INT, score BIGINT) AS (
  418. SELECT * FROM matches
  419. UNION ALL
  420. SELECT
  421. matches.card,
  422. matches.score
  423. FROM
  424. expanded,
  425. matches,
  426. generate_series(1, expanded.score) as step
  427. WHERE
  428. expanded.card + step = matches.card
  429. ),
  430. part2(part2 BIGINT) AS ( SELECT COUNT(*) FROM expanded)
  431. select * from part1, part2;
  432. ----
  433. 23 314
  434. query T multiline
  435. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  436. WITH MUTUALLY RECURSIVE
  437. -- PART 0
  438. -- Parse the input as lines of text with line numbers.
  439. lines(line TEXT) AS (
  440. SELECT regexp_split_to_table(input, '\n')
  441. FROM input
  442. ),
  443. blocks(card TEXT, wins TEXT, have TEXT) AS (
  444. SELECT
  445. TRIM (regexp_split_to_array(line, '(:|\|)')[1]),
  446. TRIM (regexp_split_to_array(line, '(:|\|)')[2]),
  447. TRIM (regexp_split_to_array(line, '(:|\|)')[3])
  448. FROM
  449. lines
  450. ),
  451. parsed(card INT, wins TEXT[], have TEXT[]) AS (
  452. SELECT
  453. regexp_match(card, '[0-9]+')[1]::INT,
  454. regexp_split_to_array(wins, ' '),
  455. regexp_split_to_array(have, ' ')
  456. FROM blocks
  457. ),
  458. -- PART 1
  459. -- Count "have"s in "wins" for each row, exponentiate, sum.
  460. matches(card INT, score BIGINT) AS (
  461. SELECT card, (
  462. SELECT COUNT(*)
  463. FROM (
  464. SELECT unnest(wins) w
  465. INTERSECT
  466. SELECT unnest(have) w
  467. )
  468. WHERE w != ''
  469. )
  470. FROM parsed
  471. ),
  472. part1(part1 NUMERIC) AS (
  473. SELECT SUM(pow(2, score - 1))::NUMERIC
  474. FROM matches
  475. WHERE score > 0
  476. ),
  477. -- PART 2
  478. -- Each card provides a copy of the next `score` cards.
  479. -- This could be prefix sum if we want to be clever ...
  480. expanded(card INT, score BIGINT) AS (
  481. SELECT * FROM matches
  482. UNION ALL
  483. SELECT
  484. matches.card,
  485. matches.score
  486. FROM
  487. expanded,
  488. matches,
  489. generate_series(1, expanded.score) as step
  490. WHERE
  491. expanded.card + step = matches.card
  492. ),
  493. part2(part2 BIGINT) AS ( SELECT COUNT(*) FROM expanded)
  494. select * from part1, part2;
  495. ----
  496. Explained Query:
  497. With
  498. cte l0 =
  499. Project (#3..=#5) // { arity: 3 }
  500. Map (regexp_split_to_array["(:|\|)", case_insensitive=false](#1{line}), text_to_integer(array_index(regexp_match["[0-9]+", case_insensitive=false](btrim(array_index(#2, 1))), 1)), regexp_split_to_array[" ", case_insensitive=false](btrim(array_index(#2, 2))), regexp_split_to_array[" ", case_insensitive=false](btrim(array_index(#2, 3)))) // { arity: 6 }
  501. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](#0{input})) // { arity: 2 }
  502. ReadStorage materialize.public.aoc_1204 // { arity: 1 }
  503. cte l1 =
  504. Distinct project=[#0, #1] // { arity: 2 }
  505. Project (#1, #2) // { arity: 2 }
  506. Get l0 // { arity: 3 }
  507. cte l2 =
  508. Filter (#2 != "") // { arity: 3 }
  509. FlatMap unnest_array(#0{wins}) // { arity: 3 }
  510. Get l1 // { arity: 2 }
  511. cte l3 =
  512. Reduce group_by=[#0, #1] aggregates=[count(*)] // { arity: 3 }
  513. Project (#0, #1) // { arity: 2 }
  514. Distinct project=[#0..=#2] // { arity: 3 }
  515. Union // { arity: 3 }
  516. Get l2 // { arity: 3 }
  517. Negate // { arity: 3 }
  518. Threshold // { arity: 3 }
  519. Union // { arity: 3 }
  520. Get l2 // { arity: 3 }
  521. Negate // { arity: 3 }
  522. Filter (#2 != "") // { arity: 3 }
  523. FlatMap unnest_array(#1{have}) // { arity: 3 }
  524. Get l1 // { arity: 2 }
  525. cte l4 =
  526. Union // { arity: 3 }
  527. Get l3 // { arity: 3 }
  528. Map (0) // { arity: 3 }
  529. Union // { arity: 2 }
  530. Negate // { arity: 2 }
  531. Project (#0, #1) // { arity: 2 }
  532. Get l3 // { arity: 3 }
  533. Get l1 // { arity: 2 }
  534. cte l5 =
  535. Project (#0, #5{count}) // { arity: 2 }
  536. Join on=(#1 = #3 AND #2 = #4) type=differential // { arity: 6 }
  537. implementation
  538. %1[#0, #1]UKK » %0:l0[#1, #2]KK
  539. ArrangeBy keys=[[#1, #2]] // { arity: 3 }
  540. Get l0 // { arity: 3 }
  541. ArrangeBy keys=[[#0, #1]] // { arity: 3 }
  542. Union // { arity: 3 }
  543. Get l4 // { arity: 3 }
  544. Map (null) // { arity: 3 }
  545. Union // { arity: 2 }
  546. Negate // { arity: 2 }
  547. Project (#0, #1) // { arity: 2 }
  548. Get l4 // { arity: 3 }
  549. Get l1 // { arity: 2 }
  550. cte l6 =
  551. Reduce aggregates=[sum(power(2, bigint_to_double((#0{count} - 1))))] // { arity: 1 }
  552. Project (#1{count}) // { arity: 1 }
  553. Filter (#1{count} > 0) // { arity: 2 }
  554. Get l5 // { arity: 2 }
  555. Return // { arity: 2 }
  556. With Mutually Recursive
  557. cte l7 =
  558. Union // { arity: 2 }
  559. Get l5 // { arity: 2 }
  560. Project (#2, #3{count}) // { arity: 2 }
  561. Filter (integer_to_bigint(#2{card}) = (integer_to_bigint(#0{card}) + #4{step})) // { arity: 5 }
  562. FlatMap generate_series(1, #1{score}, 1) // { arity: 5 }
  563. CrossJoin type=differential // { arity: 4 }
  564. implementation
  565. %0:l7[×] » %1:l5[×]
  566. ArrangeBy keys=[[]] // { arity: 2 }
  567. Get l7 // { arity: 2 }
  568. ArrangeBy keys=[[]] // { arity: 2 }
  569. Get l5 // { arity: 2 }
  570. Return // { arity: 2 }
  571. With
  572. cte l8 =
  573. Reduce aggregates=[count(*)] // { arity: 1 }
  574. Project () // { arity: 0 }
  575. Get l7 // { arity: 2 }
  576. Return // { arity: 2 }
  577. CrossJoin type=differential // { arity: 2 }
  578. implementation
  579. %0[×]U » %1[×]U
  580. ArrangeBy keys=[[]] // { arity: 1 }
  581. Project (#1) // { arity: 1 }
  582. Map (double_to_numeric(#0{sum})) // { arity: 2 }
  583. Union // { arity: 1 }
  584. Get l6 // { arity: 1 }
  585. Map (null) // { arity: 1 }
  586. Union // { arity: 0 }
  587. Negate // { arity: 0 }
  588. Project () // { arity: 0 }
  589. Get l6 // { arity: 1 }
  590. Constant // { arity: 0 }
  591. - ()
  592. ArrangeBy keys=[[]] // { arity: 1 }
  593. Union // { arity: 1 }
  594. Get l8 // { arity: 1 }
  595. Map (0) // { arity: 1 }
  596. Union // { arity: 0 }
  597. Negate // { arity: 0 }
  598. Project () // { arity: 0 }
  599. Get l8 // { arity: 1 }
  600. Constant // { arity: 0 }
  601. - ()
  602. Source materialize.public.aoc_1204
  603. Target cluster: quickstart
  604. EOF