day14_common.carbon 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/14
  5. library "day14_common";
  6. import Core library "io";
  7. import library "io_utils";
  8. fn ReadXY(xy: (i32, i32)*) -> bool {
  9. return ReadInt(ref xy->0) and ConsumeChar(',') and ReadInt(ref xy->1);
  10. }
  11. fn SkipString(s: str) -> bool {
  12. return SkipNChars(s.Size() as i32);
  13. }
  14. fn Mod(a: i32, d: i32) -> i32 {
  15. let rem: i32 = a % d;
  16. return if rem < 0 then rem + d else rem;
  17. }
  18. class Robot {
  19. impl as Core.UnformedInit {}
  20. fn Read() -> Robot {
  21. returned var me: Robot;
  22. SkipString("p=");
  23. ReadXY(&me.p);
  24. SkipString(" v=");
  25. ReadXY(&me.v);
  26. SkipNewline();
  27. return var;
  28. }
  29. fn Pos[self: Self](t: i32) -> (i32, i32) {
  30. return (Mod((self.p.0 + self.v.0 * Mod(t, 101)), 101),
  31. Mod((self.p.1 + self.v.1 * Mod(t, 103)), 103));
  32. }
  33. impl as Core.Copy {
  34. fn Op[self: Self]() -> Self {
  35. return {.p = self.p, .v = self.v};
  36. }
  37. }
  38. var p: (i32, i32);
  39. var v: (i32, i32);
  40. }