Skip to content

Instantly share code, notes, and snippets.

@zackbiernat
Created December 13, 2017 18:16
Show Gist options
  • Save zackbiernat/87bff2e3b4ee659c31341d6eb730214d to your computer and use it in GitHub Desktop.
Save zackbiernat/87bff2e3b4ee659c31341d6eb730214d to your computer and use it in GitHub Desktop.
const Tower = function(n) {
this.top = -1; // push will add one to top
this.stack = [];
for (let i = n; i > 0; i--) {
this.stack.push(i);
}
}
// push
Tower.prototype.push = function(val) {
if (this.stack[this.top] < val) {
throw new Error('ERROR pushed value must be less than top of stack')
}
this.top++;
this.stack.push(val)
// this.stack[this.top] = val;
return this // return tower
}
// pop
Tower.prototype.pop = function() {
//console.log('this.top', this.top)
this.top--;
return this.stack.pop();
}
// peek
Tower.prototype.peek = function() {
return this.stack[this.top];
}
// size
Tower.prototype.size = function() {
return this.stack.length
}
const moveDonut = function(start, end) {
end.push(start.pop());
};
const move2discs = function(start, temp, end) {
moveDonut(start, temp);
moveDonut(start, end);
moveDonut(temp, end);
}
const move3Discs = function(start, temp, end) {
moveDonut(start, end);
moveDonut(start, temp);
moveDonut(end, temp);
moveDonut(start, end);
moveDonut(temp, start);
moveDonut(temp, end)
moveDonut(start, end)
}
// by looking at all three know what move needs to made that round and recurse
// repeatable pattern between odds .... and between evens?
const moveDonuts = function(start, temp, end) {
// do the thing
// n = 1
// moveDonut(start, end)
// n = 2
// moveDonut(start, temp);
// moveDonut(start, end);
// moveDonut(temp, end);
// n = 3
// moveDonut(start, end);
// moveDonut(start, temp);
// moveDonut(end, temp);
// moveDonut(start, end);
// moveDonut(temp, start);
// moveDonut(temp, end)
// moveDonut(start, end)
// n = 4
// moveDonut(start, temp);
// moveDonut(start, end);
// moveDonut(temp, end);
// moveDonut(start, temp);
// moveDonut(temp, start);
// moveDonut(start, temp);
// moveDonut(end, start);
// moveDonut(end, temp);
// moveDonut(start, temp);
// moveDonut(start, end);
// // move 3 discs from temp to end
// move3Discs(temp, start, end);
// n = 5
// moveDonut(start, end);
// moveDonut(start, temp);
// moveDonut(end, temp);
// moveDonut(start, end);
// moveDonut(temp, start);
// moveDonut(temp, end);
// moveDonut(start, end);
// // disc 4 placed on temp
// moveDonut(start, temp) // last move or a first move
// moveDonut(end, temp)
// moveDonut(end, start)
// moveDonut(temp, start)
// moveDonut(end, temp)
// moveDonut(start, end)
// moveDonut(start, temp)
// moveDonut(end, temp)
// // disc 5 placed on end
// moveDonut(start, end)
// moveDonut(temp, start)
// moveDonut(temp, end)
// moveDonut(start, end)
// moveDonut(temp, start)
// moveDonut(end, temp)
// moveDonut(end, start)
// moveDonut(temp, start)
// // place disc 4 from temp to end
// moveDonut(temp, end)
// moveDonut(start, end) // 3 disc from start to end
// moveDonut(start, temp)
// moveDonut(end, temp)
// moveDonut(start, end)
// moveDonut(temp, start)
// moveDonut(temp, end)
// moveDonut(start, end)
// alt n = 5
// move3Discs(start, temp, end) // move 3 discs to end
// moveDonut(start, temp) // place next largest disc (4) at temp
// move3Discs(end, start, temp) // move 3 discs to temp
// moveDonut(start, end) // place next largest disc (5) at end
// move3Discs(temp, end, start) // move 3 discs to start
// moveDonut(temp, end) // move disc (4) to end
// move3Discs(start, temp, end)
// n = 6
moveDonut(start, temp)
moveDonut(start, end)
moveDonut(temp, end)
moveDonut(start, temp)
moveDonut(end, start)
moveDonut(end, temp)
moveDonut(start, temp)
//move (4)
moveDonut(start, end)
// repeat move three
moveDonut(temp, end)
moveDonut(temp, start)
moveDonut(end, start)
moveDonut(temp, end)
moveDonut(start, temp)
moveDonut(start, end)
moveDonut(temp, end)
//move (5)
moveDonut(start, temp)
// repeat move four
/*ATTEMPT TO SWITCH VARIABLEs*/
//Start > temp > end
//End > start > temp
moveDonut(end, start)
moveDonut(end, temp)
moveDonut(start, temp)
moveDonut(end, start)
moveDonut(temp, end)
moveDonut(temp, start)
moveDonut(end, start)
//move (4)
moveDonut(end, temp)
// repeat move three
moveDonut(start, temp)
moveDonut(start, end)
moveDonut(temp, end)
moveDonut(start, temp)
moveDonut(end, start)
moveDonut(end, temp)
moveDonut(start, temp)
// Move (6) *************
moveDonut(start, end)
//start > temp > end
//temp > start > end
moveDonut(temp, start)
moveDonut(temp, end)
moveDonut(start, end)
moveDonut(temp, start)
moveDonut(end, temp)
moveDonut(end, start)
moveDonut(temp, start)
//move (4)
moveDonut(temp, end)
// repeat move three
moveDonut(start, end)
moveDonut(start, temp)
moveDonut(end, temp)
moveDonut(start, end)
moveDonut(temp, start)
moveDonut(temp, end)
moveDonut(start, end)
//move (5
moveDonut(temp, temp)
// repeat move four
//ATTEMPT TO SWITCH VARIABLEs
//Start > temp > end
//End > start > temp
moveDonut(end, temp)
moveDonut(end, start)
moveDonut(temp, start)
moveDonut(end, temp)
moveDonut(start, end)
moveDonut(start, temp)
moveDonut(end, temp)
//move (4)
moveDonut(end, start)
// repeat move three
moveDonut(temp, start)
moveDonut(temp, end)
moveDonut(start, end)
moveDonut(temp, start)
moveDonut(end, temp)
moveDonut(end, start)
moveDonut(temp, start)
moveDonut(temp, end)
}
const Towers = function(n) {
const A = new Tower(n); //[3, 2, 1]
const B = new Tower();
const C = new Tower();
moveDonuts(A, B, C);
return [A.stack, B.stack, C.stack];
// find which tower is the start - use peek();
// which is the end?
// the one with the next smallest is the end,
// but if they are both empty then C is end by default
// check if tower A is either odd - use size();
// moveDonuts()
// check if tower A is either even - use size();
// moveDonuts()
};
// var hi = 'hello'
// (function(n) {
// const hanoi = Towers(n);
// console.log(`${n} towers`, hanoi);
// console.log(JSON.stringify(hanoi) === '[[],[],[1]]');
// })(1);
//
// (function(n) {
// const hanoi = Towers(n);
// console.log(`${n} towers`, hanoi);
// console.log(JSON.stringify(hanoi) === '[[],[],[2,1]]');
// })(2);
//
// (function(n) {
// const hanoi = Towers(n);
// console.log(`${n} towers`, hanoi);
// console.log(JSON.stringify(hanoi) === '[[],[],[3,2,1]]');
// })(3);
// (function(n) {
// const hanoi = Towers(n);
// console.log(`${n} towers`, hanoi);
// console.log(JSON.stringify(hanoi) === '[[],[],[4,3,2,1]]');
// })(4);
(function(n) {
const hanoi = Towers(n);
console.log(`${n} towers`, hanoi);
console.log(JSON.stringify(hanoi) === '[[],[],[6,5,4,3,2,1]]');
})(6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment