-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
50 lines (44 loc) · 1.25 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { hanoiTowers } from "./hanoi-towers";
import type { Move, Tower, Towers } from "./hanoi-towers";
const towerLabels = ["A", "B", "C"];
function printTower(tower: Tower, index: number) {
console.log(towerLabels[index], tower.join(" "));
}
function printTowers(towers: Towers) {
towers.forEach(printTower);
}
function printMove(count: number, towers: Towers, { from, to }: Move) {
if (from === to) {
console.log("(0) Initial state");
return;
}
const { length } = towers[to];
if (length <= 0) {
throw new Error("Invalid move: target column is empty");
}
const disc = towers[to][length - 1];
console.log(`(${count}) Move disc ${disc} from tower ${towerLabels[from]} to ${towerLabels[to]}`);
}
function size(): number {
const sizeArg = process.argv[2].trim();
if (!/^\d{1,2}$/.test(sizeArg)) {
throw new Error("Invalid size argument");
}
return parseInt(sizeArg, 10);
}
try {
let count = 0;
for (const [towers, move] of hanoiTowers(size())) {
printMove(count++, towers, move);
printTowers(towers);
console.log();
}
} catch (error: unknown) {
const message =
error instanceof Error
? error.message
: error instanceof String
? error
: "Invalid size argument";
console.error(message);
}