day13_common.carbon 4.5 KB

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