/** * Thst is not the optimal formula, don't use it. * This is just another solution for the algorithm * Tower of Hanoi. */ if (Array.prototype.last === undefined) { Array.prototype.last = function () { return this.length ? this[this.length - 1] : 0; }; }; var a = [3, 2, 1], b = [], c = [], counter = 0; function swap(a1, a2) { a1.push(a2.pop()); } function compareAndReplace(a1, a2) { if (!a1.length && !a2.length) { return; } if (!a2.last()) { swap(a2, a1); return true; } if (!a1.last()) { swap(a1, a2); return true; } if (a1.last() > a2.last()) { swap(a1, a2); } else { swap(a2, a1); } return true; } function towerOfHanoi(a, b, c) { if (!a.length && !b.length) { return [a, b, c]; } compareAndReplace(a, b) && logger(a, b, c); compareAndReplace(a, c) && logger(a, b, c); compareAndReplace(b, c) && logger(a, b, c); return towerOfHanoi(a, b, c); } function logger(lists) { ++counter && console.log(counter, '>', 'A:', a, 'B:', b, 'C:', c); } towerOfHanoi(a, b, c); // https://gist.github.com/vidul-nikolaev-petrov/1c18c70523c19799b10f
„Времето е в нас и ние сме във времето, то нас обръща и ние него обръщаме.“ - Васил Левски