day4_common.carbon 1.4 KB

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