day8_part1.carbon 1.1 KB

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