aoc_1206.slt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_1202.md
  10. mode cockroach
  11. statement ok
  12. CREATE TABLE input (time INT, distance INT);
  13. statement ok
  14. INSERT INTO input VALUES (16, 18);
  15. statement ok
  16. INSERT INTO input VALUES (20, 20);
  17. statement ok
  18. INSERT INTO input VALUES (27, 30);
  19. statement ok
  20. INSERT INTO input VALUES (50, 47);
  21. statement ok
  22. INSERT INTO input VALUES (60, 49);
  23. statement ok
  24. INSERT INTO input VALUES (78, 62);
  25. query I
  26. WITH options AS
  27. (
  28. SELECT
  29. (floor((time - sqrt(time * time - 4 * distance)) / 2) + 1)::int low,
  30. (ceil((time + sqrt(time * time - 4 * distance)) / 2) - 1)::int hi
  31. FROM input
  32. )
  33. SELECT exp(sum(ln(hi - low + 1)))::int
  34. FROM options;
  35. ----
  36. 1180707298
  37. query T multiline
  38. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  39. WITH options AS
  40. (
  41. SELECT
  42. (floor((time - sqrt(time * time - 4 * distance)) / 2) + 1)::int low,
  43. (ceil((time + sqrt(time * time - 4 * distance)) / 2) - 1)::int hi
  44. FROM input
  45. )
  46. SELECT exp(sum(ln(hi - low + 1)))::int
  47. FROM options;
  48. ----
  49. Explained Query:
  50. With
  51. cte l0 =
  52. Reduce aggregates=[sum(lnf64(integer_to_double(((double_to_integer((ceilf64(((integer_to_double(#0{time}) + sqrtf64(integer_to_double(((#0{time} * #0{time}) - (4 * #1{distance}))))) / 2)) - 1)) - double_to_integer((floorf64(((integer_to_double(#0{time}) - sqrtf64(integer_to_double(((#0{time} * #0{time}) - (4 * #1{distance}))))) / 2)) + 1))) + 1))))] // { arity: 1 }
  53. ReadStorage materialize.public.input // { arity: 2 }
  54. Return // { arity: 1 }
  55. Project (#1) // { arity: 1 }
  56. Map (double_to_integer(expf64(#0{sum}))) // { arity: 2 }
  57. Union // { arity: 1 }
  58. Get l0 // { arity: 1 }
  59. Map (null) // { arity: 1 }
  60. Union // { arity: 0 }
  61. Negate // { arity: 0 }
  62. Project () // { arity: 0 }
  63. Get l0 // { arity: 1 }
  64. Constant // { arity: 0 }
  65. - ()
  66. Source materialize.public.input
  67. Target cluster: quickstart
  68. EOF
  69. query I
  70. WITH MUTUALLY RECURSIVE
  71. ties(slower NUMERIC, faster NUMERIC) AS (
  72. SELECT
  73. (time + sqrt(time * time - 4 * distance)) / 2 as slower,
  74. (time - sqrt(time * time - 4 * distance)) / 2 as faster
  75. FROM input
  76. ),
  77. options(choices NUMERIC) AS (
  78. SELECT 1 + FLOOR(slower)::NUMERIC - CEIL(faster)::NUMERIC FROM ties
  79. ),
  80. part12(part12 NUMERIC) AS (
  81. SELECT pow(10.0, SUM(log(choices))) FROM options
  82. )
  83. SELECT * FROM part12;
  84. ----
  85. 1180707528
  86. query T multiline
  87. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  88. WITH MUTUALLY RECURSIVE
  89. ties(slower NUMERIC, faster NUMERIC) AS (
  90. SELECT
  91. (time + sqrt(time * time - 4 * distance)) / 2 as slower,
  92. (time - sqrt(time * time - 4 * distance)) / 2 as faster
  93. FROM input
  94. ),
  95. options(choices NUMERIC) AS (
  96. SELECT 1 + FLOOR(slower)::NUMERIC - CEIL(faster)::NUMERIC FROM ties
  97. ),
  98. part12(part12 NUMERIC) AS (
  99. SELECT pow(10.0, SUM(log(choices))) FROM options
  100. )
  101. SELECT * FROM part12;
  102. ----
  103. Explained Query:
  104. With
  105. cte l0 =
  106. Reduce aggregates=[sum(log10numeric(((1 + floornumeric(double_to_numeric(((integer_to_double(#0{time}) + sqrtf64(integer_to_double(((#0{time} * #0{time}) - (4 * #1{distance}))))) / 2)))) - ceilnumeric(double_to_numeric(((integer_to_double(#0{time}) - sqrtf64(integer_to_double(((#0{time} * #0{time}) - (4 * #1{distance}))))) / 2))))))] // { arity: 1 }
  107. ReadStorage materialize.public.input // { arity: 2 }
  108. Return // { arity: 1 }
  109. Project (#1) // { arity: 1 }
  110. Map (power_numeric(10, #0{sum})) // { arity: 2 }
  111. Union // { arity: 1 }
  112. Get l0 // { arity: 1 }
  113. Map (null) // { arity: 1 }
  114. Union // { arity: 0 }
  115. Negate // { arity: 0 }
  116. Project () // { arity: 0 }
  117. Get l0 // { arity: 1 }
  118. Constant // { arity: 0 }
  119. - ()
  120. Source materialize.public.input
  121. Target cluster: quickstart
  122. EOF