Commit 8de6360

Bryon Fryer <bfryer@wk-bfryer-17.ltsnet.net>
2023-01-05 22:22:53
day 2
1 parent c1b194a
2022/clj/.gitignore
@@ -0,0 +1,2 @@
+.clj-kondo
+.lsp
2022/clj/02_p1s.clj
@@ -1,7 +1,7 @@
+(require '[clojure.string :as str])
+
 (def e1i (slurp "../input/02-e1i.txt"))
 (def p1i (slurp "../input/02-p1i.txt"))
-;(def e2i (slurp "../input/01-e2i.txt"))
-;(def p2i (slurp "../input/01-p2i.txt"))
 
 (def loss 0)
 (def draw 3)
@@ -10,10 +10,10 @@
 (def paper 2)
 (def scissor 3)
 
-
 (defn score
   "Score for user given RPS input"
-  [opponent-choice user-choice]
+  [input]
+  (let [opponent-choice (nth input 0) user-choice (nth input 1)]
   (+ user-choice
     (cond
       (= opponent-choice user-choice) draw
@@ -22,15 +22,32 @@
       (and (= opponent-choice paper) (= user-choice rock)) loss
       (and (= opponent-choice paper) (= user-choice scissor)) win
       (and (= opponent-choice scissor) (= user-choice paper)) loss
-      (and (= opponent-choice scissor) (= user-choice rock)) win )))
+      (and (= opponent-choice scissor) (= user-choice rock)) win ))))
+
+(defn RPS
+  [char]
+  (cond
+    (= char "A") rock
+    (= char "B") paper
+    (= char "C") scissor
+    (= char "X") rock
+    (= char "Y") paper
+    (= char "Z") scissor))
+
+(defn translate
+  "translate input values into RPS, part 1"
+  [coll]
+  (map RPS coll))
 
 (defn p1s
   ([input]
    (->> input
         str/split-lines             ; newline split
-        )))
+        (map #(str/split % #" "))   ; split on space
+        (map #(translate %))     ; turn ascii into RPS values
+        (map #(score %))            ; score each round
+        (apply +))))                ; sum scores
+
 
-(println (p1s e1i))
-;(println (score rock paper)
-;(score paper rock)
-;(score scissor scissor))
+(println "e1i" (p1s e1i))
+(println "p1i" (p1s p1i))
2022/clj/02_p2s.clj
@@ -0,0 +1,62 @@
+(require '[clojure.string :as str])
+
+(def e2i (slurp "../input/02-e2i.txt"))
+(def p2i (slurp "../input/02-p2i.txt"))
+
+(def loss 0)
+(def draw 3)
+(def win 6)
+(def rock 1)
+(def paper 2)
+(def scissor 3)
+
+(defn score
+  "Score for user given RPS input"
+  [input]
+  (let [opponent-choice (nth input 0) user-choice (nth input 1)]
+  (+ user-choice
+    (cond
+      (= opponent-choice user-choice) draw
+      (and (= opponent-choice rock) (= user-choice scissor)) loss
+      (and (= opponent-choice rock) (= user-choice paper)) win
+      (and (= opponent-choice paper) (= user-choice rock)) loss
+      (and (= opponent-choice paper) (= user-choice scissor)) win
+      (and (= opponent-choice scissor) (= user-choice paper)) loss
+      (and (= opponent-choice scissor) (= user-choice rock)) win ))))
+
+(defn RPS
+  [char]
+  (cond
+    (= char "A") rock
+    (= char "B") paper
+    (= char "C") scissor
+    (= char "X") loss
+    (= char "Y") draw
+    (= char "Z") win))
+
+(defn translate
+  "translate input values into RPS, part 2"
+  [input]
+  (let [opponent-choice (RPS (nth input 0)) outcome (RPS (nth input 1))]
+    (cond
+      (= outcome draw) [opponent-choice opponent-choice]
+      (and (= opponent-choice rock) (= outcome loss)) [opponent-choice scissor]
+      (and (= opponent-choice rock) (= outcome win)) [opponent-choice paper]
+      (and (= opponent-choice paper) (= outcome loss)) [opponent-choice rock]
+      (and (= opponent-choice paper) (= outcome win)) [opponent-choice scissor]
+      (and (= opponent-choice scissor) (= outcome loss)) [opponent-choice paper]
+      (and (= opponent-choice scissor) (= outcome win)) [opponent-choice rock]
+      )))
+
+(defn p2s
+  ([input]
+   (->> input
+        str/split-lines             ; newline split
+        (map #(str/split % #" "))   ; split on space
+        (map #(translate %))        ; turn ascii into values
+        (map #(score %))            ; score each round
+        (apply +)
+        )))                ; sum scores
+
+(println "e2i" (p2s e2i))
+(println "p2i" (p2s p2i))
2022/input/02-e2i.txt
@@ -0,0 +1,1 @@
+02-e1i.txt
\ No newline at end of file
2022/input/02-p2i.txt
@@ -0,0 +1,1 @@
+02-p1i.txt
\ No newline at end of file