day4_common.carbon 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/4
  5. library "day4_common";
  6. import library "io_utils";
  7. class Wordsearch {
  8. impl as Core.UnformedInit {}
  9. fn Read() -> Wordsearch {
  10. returned var s: Wordsearch;
  11. // TODO: Use for loops once they're implemented.
  12. var y: i32 = 0;
  13. while (y < 140) {
  14. var x: i32 = 0;
  15. while (x < 140) {
  16. // TODO: Assert on failure.
  17. s.grid[x][y] = ReadChar() as i32;
  18. ++x;
  19. }
  20. // TODO: Assert on failure.
  21. SkipNewline();
  22. ++y;
  23. }
  24. return var;
  25. }
  26. fn At[self: Self](x: i32, y: i32) -> i32 {
  27. return if x < 0 or x >= 140 or y < 0 or y >= 140 then -1 else self.grid[x][y];
  28. }
  29. // TODO: Make this generic in the length of the search query.
  30. fn Check4[self: Self](xmas: array(i32, 4), x: i32, y: i32, dx: i32, dy: i32) -> bool {
  31. var i: i32 = 0;
  32. while (i < 4) {
  33. if (self.At(x + i * dx, y + i * dy) != xmas[i]) {
  34. return false;
  35. }
  36. ++i;
  37. }
  38. return true;
  39. }
  40. fn Check3[self: Self](mas: array(i32, 3), x: i32, y: i32, dx: i32, dy: i32) -> bool {
  41. var i: i32 = 0;
  42. while (i < 3) {
  43. if (self.At(x + i * dx, y + i * dy) != mas[i]) {
  44. return false;
  45. }
  46. ++i;
  47. }
  48. return true;
  49. }
  50. var grid: array(array(i32, 140), 140);
  51. }