diff --git a/src/day02.clj b/src/day02.clj index 9f28250..7a1f7fc 100644 --- a/src/day02.clj +++ b/src/day02.clj @@ -4,7 +4,7 @@ [utils] [clojure.string :as str])) -(def contents ":: String" (utils/read-file "02" true)) +(def contents ":: String" (utils/read-file "02" false)) (def lines ":: Vector of String" (str/split-lines contents)) ; cubes: red, green, blue @@ -55,11 +55,27 @@ game-sections-vec (map parse-single-game game-sections)] {:id game-id :games game-sections-vec})) +(defn game-possible? + ":: Game -> Boolean + + Checks if `game` is possible with the given color counts." + [game red-count green-count blue-count] + ; next-game :: {:red Int :green Int :blue Int} + (nil? (utils/find-first (fn [next-game] + ; find the first game that couldnt be possible + (or + (< red-count (next-game :red 0)) + (< green-count (next-game :green 0)) + (< blue-count (next-game :blue 0)))) + (game :games)))) + (defn part1 ":: () -> String" [] - (let [count (map parse-game lines)] - (str "count: " count))) + (let [games (map parse-game lines) + possible-games (filter (fn [game] (game-possible? game 12 13 14)) games) + possible-games-sum (reduce #(+ %1 (%2 :id)) 0 possible-games)] + (str possible-games-sum))) (defn part2 ":: () -> String"