Skip to content

Instantly share code, notes, and snippets.

@germ13
Created March 2, 2018 08:24
Show Gist options
  • Save germ13/6cbaeb7bd5cc9ca39539c081be9bf7c0 to your computer and use it in GitHub Desktop.
Save germ13/6cbaeb7bd5cc9ca39539c081be9bf7c0 to your computer and use it in GitHub Desktop.
Codewars Kata: Shell Game (Clojure)
(ns the-shell-game)
;;https://www.codewars.com/kata/the-shell-game/clojure
(defn switch [location swap]
(cond (= location (first swap))
(last swap)
(= location (last swap))
(first swap)
:else
location))
(defn find-the-ball [start swaps]
"Given the starting position and a list of swaps, find the final position"
(if (nil? (first swaps))
start
(find-the-ball
(switch start (first swaps))
(rest swaps))))
@germ13
Copy link
Author

germ13 commented Mar 2, 2018

Maybe I'm restating what you just said.
Line 18 and 19 are the new values (the new state) in the recursive call.

(switch start (first swaps)) ;; calculates the new location based on the first swap.
(rest swaps) ;; first swap was processed in previous line, only process remaining swaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment