aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-02-08 20:21:07 +0100
committerOscar Najera <hi@oscarnajera.com>2023-02-08 20:21:07 +0100
commit1a0a9714f95767bdad046dbf32182967fcefb5f9 (patch)
treebe3ae01c99d91cfee295e6ed893816d804fb6fde /AoC2022
parent0b36acbe058a0afb63f2c35ce8cc24fca042e8f3 (diff)
downloadscratch-1a0a9714f95767bdad046dbf32182967fcefb5f9.tar.gz
scratch-1a0a9714f95767bdad046dbf32182967fcefb5f9.tar.bz2
scratch-1a0a9714f95767bdad046dbf32182967fcefb5f9.zip
day02
Diffstat (limited to 'AoC2022')
-rw-r--r--AoC2022/02/solver.lisp33
-rw-r--r--AoC2022/aocclj/src/aocclj/day02.clj46
-rw-r--r--AoC2022/aocclj/test/aocclj/core_test.clj7
3 files changed, 66 insertions, 20 deletions
diff --git a/AoC2022/02/solver.lisp b/AoC2022/02/solver.lisp
index 52f611b..df1bedc 100644
--- a/AoC2022/02/solver.lisp
+++ b/AoC2022/02/solver.lisp
@@ -3,6 +3,9 @@
(defconstant results '((rock . scissors) (scissors . paper) (paper . rock))
"Win-lose pairs")
+(defun looses-to (play) (cdr (assoc play results)))
+(defun wins-to (play) (car (rassoc play results)))
+
(defun translate (play)
(case play
((A X) 'rock)
@@ -17,35 +20,25 @@
(defun match (my other)
(cond
+ ((eq my (wins-to other)) (values 6 'win))
((eq my other) (values 3 'draw))
- ((eq other (cdr (assoc my results))) (values 6 'win))
(t (values 0 'loose))))
-(defun strategy (play)
- (ecase play
- (X 'loose)
- (Y 'draw)
- (Z 'win)))
-
-(defun pick-move (strategy other)
- (ecase strategy
- (loose (cdr (assoc other results)))
- (draw other)
- (win (car (rassoc other results)))))
-
-(defun fixed-plays (oponent my)
- (declare (ignore oponent))
+(defun fixed-plays (my other)
+ (declare (ignore other))
(translate my))
-(defun reactive-plays (oponent my)
- (pick-move (strategy my) oponent))
+(defun reactive-plays (my other)
+ (ecase my
+ (X (looses-to other))
+ (Y other)
+ (Z (wins-to other))))
(defun solver (strategy)
(with-open-file (in "input")
- (loop :for n :from 0
- :for op = (translate (read in nil nil))
+ (loop :for op = (translate (read in nil nil))
:while op
- :for my = (funcall strategy op (read in nil nil))
+ :for my = (funcall strategy (read in nil nil) op)
:sum (+ (weight my) (match my op)))))
(fiveam:test results
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))
diff --git a/AoC2022/aocclj/test/aocclj/core_test.clj b/AoC2022/aocclj/test/aocclj/core_test.clj
index fd34fe0..c42285f 100644
--- a/AoC2022/aocclj/test/aocclj/core_test.clj
+++ b/AoC2022/aocclj/test/aocclj/core_test.clj
@@ -1,6 +1,7 @@
(ns aocclj.core-test
(:require [clojure.test :refer :all]
[aocclj.day01 :as day01]
+ [aocclj.day02 :as day02]
[aocclj.day03 :as day03]
[aocclj.day04 :as day04]
[aocclj.core :refer :all]
@@ -12,6 +13,12 @@
75622 day01/part1
213159 day01/part2)))
+(deftest day02
+ (let [input (str/split-lines (slurp "../02/input"))]
+ (are [expected function] (= expected (day02/solver function input))
+ 12535 day02/static-play
+ 15457 day02/reactive-play)))
+
(deftest day03
(let [input (str/split-lines (slurp "../03/input"))]
(are [expected function] (= expected (function input))