Created
August 7, 2012 02:02
-
-
Save amalloy/3280677 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
(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