day13_common.carbon 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // https://adventofcode.com/2024/day/13
  5. library "day13_common";
  6. import Core library "io";
  7. import library "io_utils";
  8. // Returns m and n so that am + bn = gcd.
  9. fn Euclid(a: i64, b: i64) -> {.m: i64, .n: i64, .gcd: i64} {
  10. if (a < b) {
  11. let reverse: {.m: i64, .n: i64, .gcd: i64} = Euclid(b, a);
  12. return {.m = reverse.n, .n = reverse.m, .gcd = reverse.gcd};
  13. }
  14. if (b == 0) {
  15. return {.m = 1, .n = 0, .gcd = a};
  16. }
  17. let next: {.m: i64, .n: i64, .gcd: i64} = Euclid(b, a % b);
  18. return {.m = next.n, .n = next.m - next.n * (a / b), .gcd = next.gcd};
  19. }
  20. class Machine {
  21. fn Read() -> Machine {
  22. returned var me: Machine;
  23. // "Button A: X+"
  24. SkipNChars(12);
  25. ReadInt64(&me.a.0);
  26. // ", Y+"
  27. SkipNChars(4);
  28. ReadInt64(&me.a.1);
  29. // "\nButton B: X+"
  30. SkipNChars(13);
  31. ReadInt64(&me.b.0);
  32. // ", Y+"
  33. SkipNChars(4);
  34. ReadInt64(&me.b.1);
  35. // "\nPrize: X="
  36. SkipNChars(10);
  37. ReadInt64(&me.prize.0);
  38. // ", Y="
  39. SkipNChars(4);
  40. ReadInt64(&me.prize.1);
  41. SkipNewline();
  42. return var;
  43. }
  44. var a: (i64, i64);
  45. var b: (i64, i64);
  46. var prize: (i64, i64);
  47. }
  48. // Set of solutions to 'm a + n b = c'.
  49. class BezoutSolutionSet {
  50. fn Make(a: i64, b: i64, c: i64) -> BezoutSolutionSet {
  51. var e: {.m: i64, .n: i64, .gcd: i64} = Euclid(a, b);
  52. if (c % e.gcd != 0) {
  53. // Impossible.
  54. return {.m0 = -1, .n0 = -1, .m_step = -1, .n_step = -1};
  55. }
  56. // Find an initial solution. Note that m and n might be negative.
  57. let num_gcds: i64 = c / e.gcd;
  58. e.m = e.m * num_gcds;
  59. e.n = e.n * num_gcds;
  60. // Pick the smallest positive m we can.
  61. let a_over_gcd: i64 = a / e.gcd;
  62. let b_over_gcd: i64 = b / e.gcd;
  63. var adj: i64 = 0;
  64. // This is e.m / b rounded towards -inf.
  65. // TODO: Should there be a way of expressing this directly?
  66. if (e.m < 0) {
  67. adj = (e.m - b_over_gcd + 1) / b_over_gcd;
  68. } else {
  69. adj = e.m / b_over_gcd;
  70. }
  71. e.m = e.m - adj * b_over_gcd;
  72. e.n = e.n + adj * a_over_gcd;
  73. return {.m0 = e.m, .n0 = e.n,
  74. .m_step = b_over_gcd, .n_step = -a_over_gcd};
  75. }
  76. fn Valid[self: Self]() -> bool {
  77. return self.m_step >= 0;
  78. }
  79. fn Solution[self: Self](k: i64) -> (i64, i64) {
  80. return (self.m0 + k * self.m_step, self.n0 + k * self.n_step);
  81. }
  82. // m_k * a + n_k * b == c, where:
  83. // m_k = m0 + k * m_step
  84. // n_k = n0 * k * n_step
  85. // m0 is the minimum non-negative m value, and n_step is negative.
  86. var m0: i64;
  87. var n0: i64;
  88. var m_step: i64;
  89. var n_step: i64;
  90. }
  91. // Given two sets of points s and t, find the intersection in the first quadrant
  92. // with the minimum x coordinate. Returns the intersection point, or (-1, -1) if
  93. // there is no intersection.
  94. fn FirstIntersection(s: BezoutSolutionSet, t: BezoutSolutionSet) -> (i64, i64) {
  95. if (not s.Valid() or not t.Valid()) {
  96. // One of the sets is empty.
  97. return (-1, -1);
  98. }
  99. // Easy case: lines meet at a point. This happens unless the lines are
  100. // parallel.
  101. let d: i64 = s.m_step * t.n_step - t.m_step * s.n_step;
  102. if (d != 0) {
  103. let u: i64 = (t.m0 - s.m0) * t.n_step - (t.n0 - s.n0) * t.m_step;
  104. if (u % d != 0) {
  105. // Lines don't meet at an integer point.
  106. return (-1, -1);
  107. }
  108. let j: i64 = u / d;
  109. if (j < 0 or s.n0 + j * s.n_step < 0) {
  110. // Lines don't meet in first quadrant.
  111. return (-1, -1);
  112. }
  113. return s.Solution(j);
  114. }
  115. // Hard case: lines are parallel. We also get here if either "line" is a
  116. // point, but that doesn't happen in this exercise. We know all integer points
  117. // on the line are solutions, so the first intersection is the greater of the
  118. // first point in s and the first point in t, if that point is on both lines.
  119. if (s.m0 < t.m0) {
  120. // (t.m0, t.n0) is the solution if it's in s.
  121. if ((t.m0 - s.m0) % s.m_step == 0 and
  122. (t.n0 - s.n0) % s.n_step == 0) {
  123. return (t.m0, t.n0);
  124. }
  125. } else {
  126. // (s.m0, s.n0) is the solution if it's in t.
  127. if ((s.m0 - t.m0) % t.m_step == 0 and
  128. (s.n0 - t.n0) % t.n_step == 0) {
  129. return (s.m0, s.n0);
  130. }
  131. }
  132. return (-1, -1);
  133. }
  134. fn CostIfPossible(m: Machine) -> i64 {
  135. let x: BezoutSolutionSet = BezoutSolutionSet.Make(m.a.0, m.b.0, m.prize.0);
  136. let y: BezoutSolutionSet = BezoutSolutionSet.Make(m.a.1, m.b.1, m.prize.1);
  137. let ab: (i64, i64) = FirstIntersection(x, y);
  138. if (ab.0 == -1) { return 0; }
  139. return ab.0 * 3 + ab.1;
  140. }