Skip to content

Instantly share code, notes, and snippets.

@esciafardini
Last active December 2, 2023 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esciafardini/c036c14e4ff6e10949ba8104cd773cb1 to your computer and use it in GitHub Desktop.
Save esciafardini/c036c14e4ff6e10949ba8104cd773cb1 to your computer and use it in GitHub Desktop.
(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