Skip to content

Commit

Permalink
feat(games-hanoi): implement towers of hanoi & references to Prolog s…
Browse files Browse the repository at this point in the history
…olution
  • Loading branch information
uldissturms committed Jul 29, 2019
1 parent cb9d7a0 commit 9618fa0
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ More information about hashign on [wikipedia](https://en.wikipedia.org/wiki/Hash
* ./interview-io/word-break.test.js
* ./interview-io/shortest-continuous-subarray.test.js

### Games
* ./games/tower-of-hanoi.test.js

### Sources
* https://leetcode.com
* http://www.crackingthecodinginterview.com
Expand Down
115 changes: 115 additions & 0 deletions games/tower-of-hanoi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Game - https://www.mathsisfun.com/games/towerofhanoi.html
* Prolog - https://www.cpp.edu/~jrfisher/www/prolog_tutorial/2_3.html
* Prolog (github) - https://github.com/dvberkel/prolog-hanoi
*/

import test from 'ava'
import { clone, id } from '../helpers'

let count = 0

const countMoves = (t) => {
count += 1
}

test.beforeEach(t => {
count = 0
})

test('tower of hanoi - 1', t => {
t.deepEqual(
solve([
clone([1]), [], [] ],
countMoves
),
[ [], [], clone([1]) ]
)
t.is(count, 1)
})

test('tower of hanoi - 2', t => {
t.deepEqual(
solve(
[ clone([1, 2]), [], [] ],
countMoves
),
[ [], [], clone([1, 2]) ]
)
t.is(count, 3)
})

test('tower of hanoi - 3', t => {
const towerOf3 = [1, 2, 3]
t.deepEqual(
solve(
[ clone(towerOf3), [], [] ],
countMoves
),
[ [], [], clone(towerOf3) ]
)
t.is(count, 7)
})

test('tower of hanoi - 5', t => {
const towerOf5 = [1, 2, 3, 4, 5]
t.deepEqual(
solve(
[ clone(towerOf5), [], [] ],
countMoves
),
[ [], [], clone(towerOf5) ]
)
t.is(count, 31)
})

const left = (tower) => tower[0]
const center = (tower) => tower[1]
const right = (tower) => tower[2]

const solve = (tower, onMove = id) => {
const [first] = tower
const levels = first.length
move(levels - 1, left, right, center, tower, onMove)
return tower
}

/* manual solution
* one (moves: 1)
* [1], [], []
*
* [], [], [1]
*
* two (moves: 3)
* [1, 2], [], []
* [2], [1], []
* [], [1], [2]
* [], [], [1, 2]
* three (moves: 7)
* [1, 2, 3], [], []
* [2, 3], [], [1]
* [3], [2], [1]
* [3], [1, 2], []
* [], [1, 2], [3]
* [1], [2], [3]
* [1], [], [2, 3]
* [], [], [1, 2, 3]
*/

const move = (index, left, right, center, tower, onMove) => {
if (index === 0) {
const item = left(tower).pop()
right(tower).push(item)
onMove()
return
}

move(index - 1, left, center, right, tower, onMove)
move(0, left, right, center, tower, onMove)
move(index - 1, center, right, left, tower, onMove)
}

0 comments on commit 9618fa0

Please sign in to comment.