Created
June 25, 2020 15:46
-
-
Save SVMBrown/155230d3fb6ad04971f2a3a958b7f344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def right | |
{:N :E | |
:E :S | |
:S :W | |
:W :N}) | |
(def left | |
(clojure.set/map-invert right)) | |
(defn bound [bottom top n] | |
(max bottom (min top n))) | |
(defn run-instruction [[plateau-x plateau-y] [_ _ heading :as pos] instruction] | |
(if (= :M instruction) | |
(case heading | |
;; Horizontal | |
:E (update pos 0 (comp (partial bound 0 plateau-x) inc)) | |
:W (update pos 0 (comp (partial bound 0 plateau-x) dec)) | |
;; Vertical | |
:N (update pos 1 (comp (partial bound 0 plateau-y) inc)) | |
:S (update pos 1 (comp (partial bound 0 plateau-y) dec))) | |
(update pos 2 (case instruction | |
:L left | |
:R right)))) | |
(defn move-rover [plateau {:keys [position instructions]}] | |
{:position (reduce (partial run-instruction plateau) position instructions)}) | |
(defn go! [{:keys [plateau rovers]}] | |
{:rovers (mapv (partial move-rover plateau) rovers)}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment