import { LoadInput } from "../common.ts";
import { ParseMap, GetNextNodeInMap, type MapNodeTree } from "./8_common.ts";

const tests_EZ = [
    "RL",
    "",
    "AAA = (BBB, CCC)",
    "BBB = (DDD, EEE)",
    "CCC = (ZZZ, GGG)",
    "DDD = (DDD, DDD)",
    "EEE = (EEE, EEE)",
    "GGG = (GGG, GGG)",
    "ZZZ = (ZZZ, ZZZ)",
];

const tests_longer = [
    "LLR",
    "",
    "AAA = (BBB, BBB)",
    "BBB = (AAA, ZZZ)",
    "ZZZ = (ZZZ, ZZZ)",
];

const START_NODE: keyof MapNodeTree = "AAA";
const END_NODE: keyof MapNodeTree = "ZZZ";

const input = await LoadInput(8);

const [directions, map] = ParseMap(input);

let nodeID: keyof MapNodeTree|undefined = START_NODE;
let stepsTaken = 0;

while (nodeID = GetNextNodeInMap(directions, map, nodeID, stepsTaken)) {
    stepsTaken++;
    if (nodeID == END_NODE) {
        break;
    }
}

console.log(`A total of ${stepsTaken} steps were taken`);