Skip to content

Instantly share code, notes, and snippets.

@zeroDivisible
Created December 11, 2017 23:46
Show Gist options
  • Save zeroDivisible/bf75d84b8ad73e00215e3a2ade4a11ab to your computer and use it in GitHub Desktop.
Save zeroDivisible/bf75d84b8ad73e00215e3a2ade4a11ab to your computer and use it in GitHub Desktop.
(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