diff options
author | Oscar Najera <hi@oscarnajera.com> | 2023-02-08 20:21:07 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2023-02-08 20:21:07 +0100 |
commit | 1a0a9714f95767bdad046dbf32182967fcefb5f9 (patch) | |
tree | be3ae01c99d91cfee295e6ed893816d804fb6fde /AoC2022/aocclj/src | |
parent | 0b36acbe058a0afb63f2c35ce8cc24fca042e8f3 (diff) | |
download | scratch-1a0a9714f95767bdad046dbf32182967fcefb5f9.tar.gz scratch-1a0a9714f95767bdad046dbf32182967fcefb5f9.tar.bz2 scratch-1a0a9714f95767bdad046dbf32182967fcefb5f9.zip |
day02
Diffstat (limited to 'AoC2022/aocclj/src')
-rw-r--r-- | AoC2022/aocclj/src/aocclj/day02.clj | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/AoC2022/aocclj/src/aocclj/day02.clj b/AoC2022/aocclj/src/aocclj/day02.clj new file mode 100644 index 0000000..73099d8 --- /dev/null +++ b/AoC2022/aocclj/src/aocclj/day02.clj @@ -0,0 +1,46 @@ +(ns aocclj.day02 + (:require [clojure.set :as set] + [clojure.string :as str])) + +(def loses-to {:rock :scissors :scissors :paper :paper :rock}) +(def wins-to (set/map-invert loses-to)) + +(defn translate [play] + (case play + (A X) :rock + (B Y) :paper + (C Z) :scissors)) + +(defn weight [play] + (case play + :rock 1 + :paper 2 + :scissors 3)) + +(defn match [my other] + (cond + (= my (wins-to other)) 6 + (= my other) 3 + :else 0)) + +(defn static-play [my-instruction other] + (translate my-instruction)) + +(defn reactive-play [my-instruction other] + (case my-instruction + X (loses-to other) + Y other + Z (wins-to other))) + +(defn score [strategy game-play] + (let [[oponent my] game-play + oponent (translate oponent) + my-play (strategy my oponent)] + (+ (weight my-play) (match my-play oponent)))) + +(defn solver [strategy input] + (transduce + (comp + (map #(map symbol (str/split % #" "))) + (map (partial score strategy))) + + input)) |