12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- export function ParseMap(map: string[]): [string, MapNodeTree] {
- const tree: MapNodeTree = {};
- const directions = map.shift() || "";
-
- map.shift();
- let nodeLine:string|undefined = "";
- while(nodeLine = map.shift()) {
- const [id, leftNode, rightNode] = Array.from(nodeLine.matchAll(/(\w{3})/g), (match) => match[1]);
- if (!id || !leftNode || !rightNode) {
- throw new Error(`Failed to parse line: ${nodeLine}\n\tid: ${id}\n\tleftNode: ${leftNode}\n\trightNode: ${rightNode}`);
- }
- tree[id] = {L: leftNode, R: rightNode};
- }
- return [directions, tree];
- }
- export function GetNextNodeInMap(directions: string, map: MapNodeTree, currentNode: keyof MapNodeTree, stepsTaken: number): keyof MapNodeTree|undefined {
- let nextNodeID: keyof MapNodeTree|undefined = undefined;
-
- if (map[currentNode]) {
-
- const nextDirection = directions[stepsTaken % directions.length] as "L"|"R";
-
- nextNodeID = map[currentNode][nextDirection];
- }
- return nextNodeID;
- }
- export type MapNodeTree = {
-
- [NodeID: string]: {
-
- L: string,
-
- R: string,
- },
- };
|