8_1.ts 894 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { LoadInput } from "../common.ts";
  2. import { ParseMap, GetNextNodeInMap, type MapNodeTree } from "./8_common.ts";
  3. const tests_EZ = [
  4. "RL",
  5. "",
  6. "AAA = (BBB, CCC)",
  7. "BBB = (DDD, EEE)",
  8. "CCC = (ZZZ, GGG)",
  9. "DDD = (DDD, DDD)",
  10. "EEE = (EEE, EEE)",
  11. "GGG = (GGG, GGG)",
  12. "ZZZ = (ZZZ, ZZZ)",
  13. ];
  14. const tests_longer = [
  15. "LLR",
  16. "",
  17. "AAA = (BBB, BBB)",
  18. "BBB = (AAA, ZZZ)",
  19. "ZZZ = (ZZZ, ZZZ)",
  20. ];
  21. const START_NODE: keyof MapNodeTree = "AAA";
  22. const END_NODE: keyof MapNodeTree = "ZZZ";
  23. const input = await LoadInput(8);
  24. const [directions, map] = ParseMap(input);
  25. let nodeID: keyof MapNodeTree|undefined = START_NODE;
  26. let stepsTaken = 0;
  27. while (nodeID = GetNextNodeInMap(directions, map, nodeID, stepsTaken)) {
  28. stepsTaken++;
  29. if (nodeID == END_NODE) {
  30. break;
  31. }
  32. }
  33. console.log(`A total of ${stepsTaken} steps were taken`);