Skip to content

Instantly share code, notes, and snippets.

@fcanas
Last active August 29, 2015 14:20
Show Gist options
  • Save fcanas/39391b6a5670af668862 to your computer and use it in GitHub Desktop.
Save fcanas/39391b6a5670af668862 to your computer and use it in GitHub Desktop.
Shell Game
; To run:
;
; $ lein repl
; $ (load-file "shell_game_test.clj")
(ns the-shell-game-test
(:require [clojure.test :refer :all]))
(defn move-the-ball [position, move]
(nth (concat [position] (reverse move)) (+ 1 (.indexOf move position)))
)
(defn find-the-ball [start, moves]
(reduce move-the-ball start, moves)
)
(deftest basic-test
(testing "Some games"
(is (= 2 (find-the-ball 0 [[0 1] [1 2] [1 0]])))
(is (= 2 (find-the-ball 0 [[0 1] [2 1] [0 1]])))
(is (= 1 (find-the-ball 0 [[0 1]])))
(is (= 0 (find-the-ball 1 [[0 1]])))
(is (= 1 (find-the-ball 0 [[0 1] [1 2] [2 0] [0 1] [1 2] [2 1] [2 0] [0 2]])))
(is (= 2 (find-the-ball 0 [[0 2] [1 0]])))
(is (= 0 (find-the-ball 1 [[0 2] [1 0]])))
(is (= 2 (find-the-ball 0 [[0 9] [9 3] [3 7] [7 8] [8 2] [4 5]])))
(is (= 1 (find-the-ball 1 [[0 9] [9 3] [3 7] [7 8] [8 2] [4 5]])))
(is (= 8 (find-the-ball 2 [[0 9] [9 3] [3 7] [7 8] [8 2] [4 5]])))
(is (= 9 (find-the-ball 3 [[0 9] [9 3] [3 7] [7 8] [8 2] [4 5]])))
(is (= 5 (find-the-ball 4 [[0 9] [9 3] [3 7] [7 8] [8 2] [4 5]])))
(is (= 4 (find-the-ball 5 [[0 9] [9 3] [3 7] [7 8] [8 2] [4 5]])))
(is (= 6 (find-the-ball 6 [[0 9] [9 3] [3 7] [7 8] [8 2] [4 5]])))
))
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment