day8_part1.carbon 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/8
  5. import Core library "io";
  6. import Core library "range";
  7. import library "day8_common";
  8. import library "io_utils";
  9. fn IsAntinode(grid: Grid, ox: i32, oy: i32) -> bool {
  10. for (ay: i32 in Core.Range(50)) {
  11. let by: i32 = ay * 2 - oy;
  12. if (by >= 0 and by < 50) {
  13. for (ax: i32 in Core.Range(50)) {
  14. let bx: i32 = ax * 2 - ox;
  15. if (bx >= 0 and bx < 50 and (ax != bx or ay != by)) {
  16. if (grid.data[ax][ay] != 0x2E and
  17. grid.data[ax][ay] == grid.data[bx][by]) {
  18. return true;
  19. }
  20. }
  21. }
  22. }
  23. }
  24. return false;
  25. }
  26. fn CountAntinodes(grid: Grid) -> i32 {
  27. var count: i32 = 0;
  28. for (y: i32 in Core.Range(50)) {
  29. for (x: i32 in Core.Range(50)) {
  30. if (IsAntinode(grid, x, y)) {
  31. ++count;
  32. }
  33. }
  34. }
  35. return count;
  36. }
  37. fn Run() {
  38. Core.Print(CountAntinodes(Grid.Read()));
  39. }