Last active
August 8, 2024 09:48
-
-
Save likev/d56eabcaf6bac3155b9a9186c0039589 to your computer and use it in GitHub Desktop.
Tower of Hanoi in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MAX_SIZE = 5 | |
let a = [5, 4, 3, 2, 1], | |
b = [], | |
c = []; | |
let abc = { | |
a, | |
b, | |
c | |
} | |
function repeat_str(s, n) { | |
return Array(n).fill(s).join('') | |
} | |
function hnt(keys, n) { | |
let key0 = keys[0], | |
key1 = keys[1], | |
key2 = keys[2]; | |
if (n === 1) { | |
let tip = `from ${key0} to ${key2}` | |
let k = abc[key0].pop() | |
abc[key2].push(k) | |
console.log(`${tip} moving ${k}`) | |
console.log(`after moving: a:[${abc.a}] b:[${abc.b}] c:[${abc.c}]`) | |
} else { | |
console.log(repeat_str('|', MAX_SIZE - n) + repeat_str('-', 20)) | |
let move1 = `${keys[0]}${keys[2]}${keys[1]}`; | |
console.log(`hnt(${move1},${n-1})`) | |
hnt(move1, n - 1) | |
let move2 = `${keys[0]}${keys[1]}${keys[2]}`; | |
console.log(`hnt(${move2},1)`) | |
hnt(move2, 1) | |
let move3 = `${keys[1]}${keys[0]}${keys[2]}`; | |
console.log(`hnt(${move3},${n-1})`) | |
hnt(move3, n - 1) | |
console.log(repeat_str('|', MAX_SIZE - n) + repeat_str('-', 20)) | |
} | |
} | |
hnt("abc", 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------- | |
hnt(acb,4) | |
|-------------------- | |
hnt(abc,3) | |
||-------------------- | |
hnt(acb,2) | |
|||-------------------- | |
hnt(abc,1) | |
from a to c moving 1 | |
after moving: a:[5,4,3,2] b:[] c:[1] | |
hnt(acb,1) | |
from a to b moving 2 | |
after moving: a:[5,4,3] b:[2] c:[1] | |
hnt(cab,1) | |
from c to b moving 1 | |
after moving: a:[5,4,3] b:[2,1] c:[] | |
|||-------------------- | |
hnt(abc,1) | |
from a to c moving 3 | |
after moving: a:[5,4] b:[2,1] c:[3] | |
hnt(bac,2) | |
|||-------------------- | |
hnt(bca,1) | |
from b to a moving 1 | |
after moving: a:[5,4,1] b:[2] c:[3] | |
hnt(bac,1) | |
from b to c moving 2 | |
after moving: a:[5,4,1] b:[] c:[3,2] | |
hnt(abc,1) | |
from a to c moving 1 | |
after moving: a:[5,4] b:[] c:[3,2,1] | |
|||-------------------- | |
||-------------------- | |
hnt(acb,1) | |
from a to b moving 4 | |
after moving: a:[5] b:[4] c:[3,2,1] | |
hnt(cab,3) | |
||-------------------- | |
hnt(cba,2) | |
|||-------------------- | |
hnt(cab,1) | |
from c to b moving 1 | |
after moving: a:[5] b:[4,1] c:[3,2] | |
hnt(cba,1) | |
from c to a moving 2 | |
after moving: a:[5,2] b:[4,1] c:[3] | |
hnt(bca,1) | |
from b to a moving 1 | |
after moving: a:[5,2,1] b:[4] c:[3] | |
|||-------------------- | |
hnt(cab,1) | |
from c to b moving 3 | |
after moving: a:[5,2,1] b:[4,3] c:[] | |
hnt(acb,2) | |
|||-------------------- | |
hnt(abc,1) | |
from a to c moving 1 | |
after moving: a:[5,2] b:[4,3] c:[1] | |
hnt(acb,1) | |
from a to b moving 2 | |
after moving: a:[5] b:[4,3,2] c:[1] | |
hnt(cab,1) | |
from c to b moving 1 | |
after moving: a:[5] b:[4,3,2,1] c:[] | |
|||-------------------- | |
||-------------------- | |
|-------------------- | |
hnt(abc,1) | |
from a to c moving 5 | |
after moving: a:[] b:[4,3,2,1] c:[5] | |
hnt(bac,4) | |
|-------------------- | |
hnt(bca,3) | |
||-------------------- | |
hnt(bac,2) | |
|||-------------------- | |
hnt(bca,1) | |
from b to a moving 1 | |
after moving: a:[1] b:[4,3,2] c:[5] | |
hnt(bac,1) | |
from b to c moving 2 | |
after moving: a:[1] b:[4,3] c:[5,2] | |
hnt(abc,1) | |
from a to c moving 1 | |
after moving: a:[] b:[4,3] c:[5,2,1] | |
|||-------------------- | |
hnt(bca,1) | |
from b to a moving 3 | |
after moving: a:[3] b:[4] c:[5,2,1] | |
hnt(cba,2) | |
|||-------------------- | |
hnt(cab,1) | |
from c to b moving 1 | |
after moving: a:[3] b:[4,1] c:[5,2] | |
hnt(cba,1) | |
from c to a moving 2 | |
after moving: a:[3,2] b:[4,1] c:[5] | |
hnt(bca,1) | |
from b to a moving 1 | |
after moving: a:[3,2,1] b:[4] c:[5] | |
|||-------------------- | |
||-------------------- | |
hnt(bac,1) | |
from b to c moving 4 | |
after moving: a:[3,2,1] b:[] c:[5,4] | |
hnt(abc,3) | |
||-------------------- | |
hnt(acb,2) | |
|||-------------------- | |
hnt(abc,1) | |
from a to c moving 1 | |
after moving: a:[3,2] b:[] c:[5,4,1] | |
hnt(acb,1) | |
from a to b moving 2 | |
after moving: a:[3] b:[2] c:[5,4,1] | |
hnt(cab,1) | |
from c to b moving 1 | |
after moving: a:[3] b:[2,1] c:[5,4] | |
|||-------------------- | |
hnt(abc,1) | |
from a to c moving 3 | |
after moving: a:[] b:[2,1] c:[5,4,3] | |
hnt(bac,2) | |
|||-------------------- | |
hnt(bca,1) | |
from b to a moving 1 | |
after moving: a:[1] b:[2] c:[5,4,3] | |
hnt(bac,1) | |
from b to c moving 2 | |
after moving: a:[1] b:[] c:[5,4,3,2] | |
hnt(abc,1) | |
from a to c moving 1 | |
after moving: a:[] b:[] c:[5,4,3,2,1] | |
|||-------------------- | |
||-------------------- | |
|-------------------- | |
-------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment