Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created August 7, 2012 02:02
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save amalloy/3280677 to your computer and use it in GitHub Desktop.
(defn fight-one-round [[a b :as fighters]]
(let [ticks (apply min (map :cooldown-remaining fighters))
damage-dealt (fn [fighter]
(if (= ticks (:cooldown-remaining fighter))
(:dmg fighter)
0))
tick (fn [fighter]
(update-in fighter [:cooldown-remaining]
(fn [cdr]
(if (= cdr ticks)
(:cooldown fighter)
(- cdr ticks)))))]
[(tick (update-in a [:health] - (damage-dealt b)))
(tick (update-in b [:health] - (damage-dealt a)))]))
(defn fight [a b]
(first
(drop-while (fn [fighters]
(every? pos? (map :health fighters)))
(iterate fight-one-round
(for [fighter [a b]]
(assoc fighter :cooldown-remaining
(:cooldown fighter)))))))
(fight {:health 45
:cooldown 86
:dmg 6}
{:health 160
:cooldown 144
:dmg 10})
;; [{:cooldown-remaining 54, :health -5, :cooldown 86, :dmg 6}
;; {:cooldown-remaining 144, :health 112, :cooldown 144, :dmg 10}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment