Created
December 2, 2023 07:10
-
-
Save jreighley/14ec18793fe5bb9bbdee41493dbe2edd to your computer and use it in GitHub Desktop.
Advent of Code day 2
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 y2023.d2) | |
(def input (->> "input/2023/2.txt" | |
(slurp))) | |
(def cube-limits {:red 12 , :green 13, :blue 14}) | |
(defn legal-game? [game] | |
(= cube-limits (merge-with max game cube-limits))) | |
(defn cubetxt->kv [cubetxt] | |
{(keyword (re-find #"blue|green|red" cubetxt)) | |
(parse-long (re-find #"\d+" cubetxt))}) | |
(defn game-txt->map [game-txt] | |
(reduce conj (map cubetxt->kv (re-seq #"\d+ \w+" game-txt)))) | |
(defn parse-game [game-line] | |
(let [[game-label cube-sets-text] (-> game-line | |
(clojure.string/split #":")) | |
game-number (parse-long (re-find #"\d+" game-label)) | |
cube-sets (-> cube-sets-text | |
(clojure.string/split #";")) | |
game-maps (->> cube-sets | |
(map game-txt->map) | |
(map legal-game?) | |
vec)] | |
{game-number (every? true? game-maps)})) | |
(defn solve-1 [input] | |
(let [game-list (clojure.string/split-lines input)] | |
(->> game-list | |
(map parse-game) | |
(reduce conj) | |
(filter #(val %)) | |
keys | |
(reduce +)))) | |
(comment (solve-1 input)) | |
;;2369 is correct) | |
(defn parse-game2 [game-line] | |
(let [[game-label cube-sets-text] (-> game-line | |
(clojure.string/split #":")) | |
cube-sets (-> cube-sets-text | |
(clojure.string/split #";")) | |
game-powers (->> cube-sets | |
(map game-txt->map) | |
(apply merge-with max) | |
vals | |
(reduce *))] | |
game-powers)) | |
(defn solve-2 [input] | |
(let [game-list (clojure.string/split-lines input)] | |
(->> game-list | |
(map parse-game2) | |
(reduce +)))) | |
(comment ( solve-2 input)) | |
;;66363 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment