Last active
December 2, 2023 14:43
-
-
Save esciafardini/c036c14e4ff6e10949ba8104cd773cb1 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 core | |
(:require | |
[clojure.string :as string])) | |
(def max-per-color | |
{"red" 12 | |
"green" 13 | |
"blue" 14}) | |
(defn convert-it [input] | |
(let [[game results] (string/split input #":") | |
k (->> (string/split game #" ") | |
second | |
Integer/parseInt) | |
split-results (->> (string/split results #";") | |
(map (comp (fn [s] (string/split s #",")) string/trim)) | |
(mapcat (fn [vec-of-strs] (map (comp #(string/split % #" ") string/trim) vec-of-strs))) | |
(group-by second) | |
(map (fn [[k v]] [k | |
(reduce max (map (comp (fn [s] (Integer/parseInt s)) first) v))])) | |
(into {}) | |
(assoc {k nil} k))] | |
split-results)) | |
(defn check-max [[id game-map]] | |
(when (and (<= (get game-map "red") (get max-per-color "red")) | |
(<= (get game-map "blue") (get max-per-color "blue")) | |
(<= (get game-map "green") (get max-per-color "green"))) | |
id)) | |
;part 1 | |
(->> (string/split (slurp "input2") #"\n") | |
(map (comp check-max first convert-it)) | |
(remove nil?) | |
(reduce +)) | |
;part 2 | |
(->> (string/split (slurp "input2") #"\n") | |
(mapcat (comp vals convert-it)) | |
(mapv (comp (partial reduce *) vals)) | |
(reduce +)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment