Created
December 2, 2022 06:05
-
-
Save iwrotesomecode/ea0a28a1e0ad5ac55123805d45d8f35e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns aoc2022.day02 | |
(:require [clojure.java.io :as io] | |
[clojure.string :as str])) | |
(def input (slurp (io/resource "input_02.txt"))) | |
(def demo-input "A Y | |
B X | |
C Z") | |
(def defeats {:rock :scissors, | |
:paper :rock, | |
:scissors :paper}) | |
(def opponent {"A" :rock | |
"B" :paper | |
"C" :scissors}) | |
(def self {"Y" :paper | |
"X" :rock | |
"Z" :scissors}) | |
(def shape-score {:rock 1 :paper 2 :scissors 3}) | |
(def outcome-score {:lose 0 :draw 3 :win 6}) | |
(defn score-round [[opponent self]] | |
(cond | |
(= opponent self) (+ (:draw outcome-score) (get shape-score self)) | |
(= opponent (get defeats self)) (+ (:win outcome-score) (get shape-score self)) | |
(= self (get defeats opponent)) (+ (:lose outcome-score) (get shape-score self)) | |
:else 0)) | |
(defn parse-strategy [input] | |
(->> (str/split-lines input) | |
(map #(str/split % #"\s+")) | |
(map (fn [s] [(get opponent (first s)) | |
(get self (second s))])))) | |
(defn part1 [input] | |
(->> (parse-strategy input) | |
(map #(score-round %)) | |
(reduce +))) | |
(part1 input);; => 15632 | |
(defn win [op] | |
(for [[k v] defeats :when (= v op)] k)) | |
(def self2 {"Y" (partial identity) | |
"X" (partial get defeats) | |
"Z" (partial (comp first win))}) | |
(defn parse-strategy2 [input] | |
(->> (str/split-lines input) | |
(map #(str/split % #"\s+")) | |
(map (fn [s] (let [op (get opponent (first s))] | |
[op ((get self2 (second s)) op)]))))) | |
(->> (parse-strategy2 demo-input)) | |
(defn part2 [input] | |
(->> (parse-strategy2 input) | |
(map #(score-round %)) | |
(reduce +))) | |
(part2 demo-input) | |
;; => 12 | |
(part2 input) | |
;; => 14416 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment