Skip to content

Instantly share code, notes, and snippets.

@runejuhl
Created October 15, 2021 09:28
Show Gist options
  • Save runejuhl/025121ecd459542859d04fc5e70daa2b to your computer and use it in GitHub Desktop.
Save runejuhl/025121ecd459542859d04fc5e70daa2b to your computer and use it in GitHub Desktop.
scoring and summation
(defn scoring
[[s1 s2]]
(cond
(= s1 s2) [1 1]
(zero? s1) [3 0]
(pos? s1) [3 1]
:else [0 0]))
(def sum-points
(partial reduce (partial merge-with +)))
(defn calculate-scores
[scores]
(map (fn [{:keys [player1 player2 score]}]
(let [[s1 s2] score
sorted-score (sort score)
reverse? (not= score sorted-score)
points (cond-> (scoring sorted-score)
reverse? reverse)]
(apply hash-map (interleave [player1 player2] points))))
scores))
(let [scores [{:player1 "W"
:player2 "L"
:score [2 0]}
{:player1 "W"
:player2 "L"
:score [2 1]}]]
(calculate-scores scores)
;; => ({"L" 3, "W" 0} {"L" 3, "W" 1})
(sum-points (calculate-scores scores))
;; {"L" 6, "W" 1}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment