day6_part2.carbon 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/6
  5. import Core library "io";
  6. import library "day6_common";
  7. class LoopDetector {
  8. fn Make() -> LoopDetector {
  9. return {.last = ((-1, -1), (-1, -1)), .steps = 1, .next_steps = 1};
  10. }
  11. fn Check[addr self: Self*](next: ((i32, i32), (i32, i32))) -> bool {
  12. // TODO: if (next == self->last) {
  13. // TODO: The lexer mishandles `next.0.0` as `next` `.` `0.0`.
  14. if (next.0 .0 == self->last.0 .0 and next.0 .1 == self->last.0 .1 and
  15. next.1 .0 == self->last.1 .0 and next.1 .1 == self->last.1 .1) {
  16. return true;
  17. }
  18. --self->steps;
  19. if (self->steps == 0) {
  20. self->steps = self->next_steps;
  21. self->next_steps <<= 1;
  22. self->last = next;
  23. }
  24. return false;
  25. }
  26. var last: ((i32, i32), (i32, i32));
  27. var steps: i32;
  28. var next_steps: i32;
  29. }
  30. fn AddingObstacleMakesALoop(position: Maze) -> bool {
  31. var maze: Maze = position.Copy();
  32. var loop: LoopDetector = LoopDetector.Make();
  33. if (not maze.AddObstacle()) {
  34. return false;
  35. }
  36. while (maze.Step()) {
  37. if (loop.Check((maze.loc, maze.dir))) {
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. fn Run() {
  44. var maze: Maze = Maze.Read();
  45. var loops: i32 = 0;
  46. while (true) {
  47. if (AddingObstacleMakesALoop(maze)) {
  48. ++loops;
  49. }
  50. if (not maze.Step()) {
  51. break;
  52. }
  53. }
  54. Core.Print(loops);
  55. }