aoc_1201.slt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_1201.md
  10. mode cockroach
  11. statement ok
  12. CREATE TABLE aoc_1201 (input TEXT);
  13. statement ok
  14. INSERT INTO aoc_1201 VALUES
  15. ('noveneiner9'),
  16. ('seventwoseven114'),
  17. ('1two4two'),
  18. ('hell0l1'),
  19. ('79430242'),
  20. ('159a951'),
  21. ('0'),
  22. ('seven2seven'),
  23. ('h4mb5rg')
  24. ;
  25. query I
  26. SELECT SUM(LEFT(r, 1)::int * 10 + RIGHT(r, 1)::int) AS part1
  27. FROM (
  28. SELECT regexp_replace(input, '[^\d]', '', 'g') AS r
  29. FROM aoc_1201
  30. );
  31. ----
  32. 278
  33. query T multiline
  34. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  35. SELECT SUM(LEFT(r, 1)::int * 10 + RIGHT(r, 1)::int) AS part1
  36. FROM (
  37. SELECT regexp_replace(input, '[^\d]', '', 'g') AS r
  38. FROM aoc_1201
  39. );
  40. ----
  41. Explained Query:
  42. With
  43. cte l0 =
  44. Reduce aggregates=[sum(((text_to_integer(left(regexp_replace["[^\d]", case_insensitive=false, limit=0](#0{input}, ""), 1)) * 10) + text_to_integer(right(regexp_replace["[^\d]", case_insensitive=false, limit=0](#0{input}, ""), 1))))] // { arity: 1 }
  45. ReadStorage materialize.public.aoc_1201 // { arity: 1 }
  46. Return // { arity: 1 }
  47. Union // { arity: 1 }
  48. Get l0 // { arity: 1 }
  49. Map (null) // { arity: 1 }
  50. Union // { arity: 0 }
  51. Negate // { arity: 0 }
  52. Project () // { arity: 0 }
  53. Get l0 // { arity: 1 }
  54. Constant // { arity: 0 }
  55. - ()
  56. Source materialize.public.aoc_1201
  57. Target cluster: quickstart
  58. EOF
  59. query I
  60. WITH
  61. lines AS (
  62. SELECT regexp_split_to_table(input, '\n') AS line
  63. FROM aoc_1201
  64. ),
  65. slices AS (
  66. SELECT line, index, substring(line, index, width) AS slice
  67. FROM
  68. lines,
  69. generate_series(1, length(line)) AS index,
  70. generate_series(1, 5) AS width
  71. ),
  72. numbers (t, n) AS (
  73. VALUES ('0', 0), ('1', 1), ('2', 2), ('3', 3), ('4', 4), ('5', 5), ('6', 6), ('7', 7), ('8', 8), ('9', 9),
  74. ('zero', 0), ('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9)
  75. ),
  76. findings AS (
  77. SELECT line, index, n AS number
  78. FROM slices, numbers
  79. WHERE slices.slice = numbers.t
  80. ),
  81. first AS ( SELECT DISTINCT ON (line) line, number AS f FROM findings ORDER BY line, index ),
  82. last AS ( SELECT DISTINCT ON (line) line, number AS l FROM findings ORDER BY line, index DESC )
  83. SELECT SUM(f * 10 + l)
  84. FROM first, last
  85. WHERE first.line = last.line
  86. ----
  87. 391
  88. query T multiline
  89. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  90. WITH
  91. lines AS (
  92. SELECT regexp_split_to_table(input, '\n') AS line
  93. FROM aoc_1201
  94. ),
  95. slices AS (
  96. SELECT line, index, substring(line, index, width) AS slice
  97. FROM
  98. lines,
  99. generate_series(1, length(line)) AS index,
  100. generate_series(1, 5) AS width
  101. ),
  102. numbers (t, n) AS (
  103. VALUES ('0', 0), ('1', 1), ('2', 2), ('3', 3), ('4', 4), ('5', 5), ('6', 6), ('7', 7), ('8', 8), ('9', 9),
  104. ('zero', 0), ('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9)
  105. ),
  106. findings AS (
  107. SELECT line, index, n AS number
  108. FROM slices, numbers
  109. WHERE slices.slice = numbers.t
  110. ),
  111. first AS ( SELECT DISTINCT ON (line) line, number AS f FROM findings ORDER BY line, index ),
  112. last AS ( SELECT DISTINCT ON (line) line, number AS l FROM findings ORDER BY line, index DESC )
  113. SELECT SUM(f * 10 + l)
  114. FROM first, last
  115. WHERE first.line = last.line
  116. ----
  117. Explained Query:
  118. With
  119. cte l0 =
  120. Project (#0, #1, #4) // { arity: 3 }
  121. Join on=(#3{t} = substr(#0{line}, #1{index}, #2{width})) type=delta // { arity: 5 }
  122. implementation
  123. %0 » %1[×] » %2[#0{t}]UK
  124. %1 » %0[×] » %2[#0{t}]UK
  125. %2 » %0[×] » %1[×]
  126. ArrangeBy keys=[[]] // { arity: 2 }
  127. FlatMap generate_series(1, char_length(#0{line}), 1) // { arity: 2 }
  128. Project (#1) // { arity: 1 }
  129. Filter (#1{line}) IS NOT NULL // { arity: 2 }
  130. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](#0{input})) // { arity: 2 }
  131. ReadStorage materialize.public.aoc_1201 // { arity: 1 }
  132. ArrangeBy keys=[[]] // { arity: 1 }
  133. Constant // { arity: 1 }
  134. - (1)
  135. - (2)
  136. - (3)
  137. - (4)
  138. - (5)
  139. ArrangeBy keys=[[#0{t}]] // { arity: 2 }
  140. Constant // { arity: 2 }
  141. - ("0", 0)
  142. - ("1", 1)
  143. - ("2", 2)
  144. - ("3", 3)
  145. - ("4", 4)
  146. - ("5", 5)
  147. - ("6", 6)
  148. - ("7", 7)
  149. - ("8", 8)
  150. - ("9", 9)
  151. - ("one", 1)
  152. - ("six", 6)
  153. - ("two", 2)
  154. - ("zero", 0)
  155. - ("five", 5)
  156. - ("four", 4)
  157. - ("nine", 9)
  158. - ("eight", 8)
  159. - ("seven", 7)
  160. - ("three", 3)
  161. cte l1 =
  162. Reduce aggregates=[sum(((#0{f} * 10) + #1{l}))] // { arity: 1 }
  163. Project (#1, #3) // { arity: 2 }
  164. Join on=(#0{line} = #2{line}) type=differential // { arity: 4 }
  165. implementation
  166. %0[#0{line}]UK » %1[#0{line}]UK
  167. ArrangeBy keys=[[#0{line}]] // { arity: 2 }
  168. Project (#0, #2) // { arity: 2 }
  169. TopK group_by=[#0] order_by=[#1 asc nulls_last] limit=1 // { arity: 3 }
  170. Get l0 // { arity: 3 }
  171. ArrangeBy keys=[[#0{line}]] // { arity: 2 }
  172. Project (#0, #2) // { arity: 2 }
  173. TopK group_by=[#0] order_by=[#1 desc nulls_first] limit=1 // { arity: 3 }
  174. Get l0 // { arity: 3 }
  175. Return // { arity: 1 }
  176. Union // { arity: 1 }
  177. Get l1 // { arity: 1 }
  178. Map (null) // { arity: 1 }
  179. Union // { arity: 0 }
  180. Negate // { arity: 0 }
  181. Project () // { arity: 0 }
  182. Get l1 // { arity: 1 }
  183. Constant // { arity: 0 }
  184. - ()
  185. Source materialize.public.aoc_1201
  186. Target cluster: quickstart
  187. EOF