Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(defn move-hex
[point direction]
(case direction
:ne [(inc (point 0)) (dec (point 1))]
:se [(inc (point 0)) (point 1)]
:s [(point 0) (inc (point 1))]
:sw [(dec (point 0)) (inc (point 1))]
:nw [(dec (point 0)) (point 1)]
:n [(point 0) (dec (point 1))]))
(defn traverse-grid-from-origin
[directions]
(loop [point [0 0]
max-distance 0
directions directions]
(cond
(empty? directions) (max-distance point)
:else (let [new-point (move-hex point (first directions))
current-distance (max-distance new-point)]
(recur new-point
(if (>= current-distance max-distance)
current-distance
max-distance)
(rest directions))))))
(defn max-distance
[point]
(Math/max (Math/abs (point 0)) (Math/abs (point 1))))
;; this fails when running, but stepping through it works.
(traverse-grid-from-origin [:s])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment