8000 Solve day 2 2022 · motform/advent-of-clojure@0f0360f · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 0f0360f

Browse files
committed
Solve day 2 2022
1 parent e6cac4d commit 0f0360f

File tree

1 file changed

+35
-0
lines changed
  • src/advent_of_clojure/2022

1 file changed

+35
-0
lines changed

src/advent_of_clojure/2022/02.clj

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(ns advent-of-clojure.2022.02
2+
(:require [clojure.string :as str]))
3+
4+
(def input (partition 2 (-> "resources/2022/02.dat" slurp (str/split #"\s+"))))
5+
6+
;; You can probably do some smart shifting here
7+
(def draw {"A" "X", "B" "Y", "C" "Z"})
8+
(def beats {"A" "Y", "B" "Z", "C" "X"})
9+
(def loses-to {"A" "Z", "B" "X", "C" "Y"})
10+
11+
(defn outcome [opponent you]
12+
(cond (= (draw opponent) you) 3
13+
(= (beats opponent) you) 6
14+
:else 0))
15+
16+
(def value {"X" 1, "Y" 2, "Z" 3})
17+
18+
(defn score [[opponent you]]
19+
(+ (value you) (outcome opponent you)))
20+
21+
(def part-one (->> input (map score) (reduce +)))
22+
23+
(defn move [opponent outcome]
24+
(case outcome
25+
"X" (loses-to opponent)
26+
"Y" (draw opponent)
27+
"Z" (beats opponent)))
28+
29+
(def points {"X" 0 "Y" 3 "Z" 6})
30+
31+
(defn final-score [[opponent outcome]]
32+
(+ (value (move opponent outcome))
33+
(points outcome)))
34+
35+
(def part-two (->> input (map final-score) (reduce +)))

0 commit comments

Comments
 (0)
0