Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(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)
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]
(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