diff --git a/input_test/02.txt b/input_test/02.txt new file mode 100644 index 0000000..295c36d --- /dev/null +++ b/input_test/02.txt @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green diff --git a/src/day02.clj b/src/day02.clj index fd28acd..9f28250 100644 --- a/src/day02.clj +++ b/src/day02.clj @@ -2,10 +2,10 @@ "Day 02 funtions" (:require [utils] - [clojure.string])) + [clojure.string :as str])) -(def contents ":: String" (utils/read-file "01" false)) -(def lines ":: Vector of String" (clojure.string/split-lines contents)) +(def contents ":: String" (utils/read-file "02" true)) +(def lines ":: Vector of String" (str/split-lines contents)) ; cubes: red, green, blue ; the Elf will reach into the bag, grab a handful of random cubes, @@ -19,10 +19,47 @@ ; Determine which games would have been possible if the bag had been loaded ; with only 12 red cubes, 13 green cubes, and 14 blue cubes +; " 3 blue, 4 red" -> {:blue 3 :red 4} +(defn parse-single-game + ":: String -> {:blue Int :red Int :green Int}" + [input] + ; colors-string :: Vector of " " + (let [colors-string (map str/trim (str/split input #","))] + (reduce (fn [acc next-color] + ; parse the number and color + (let [space-idx (.indexOf next-color " ") + amount-str (.substring next-color 0 space-idx) + amount (Integer/parseInt amount-str) + color (.substring next-color (inc space-idx) (count next-color)) + color-keyword (case color + "red" :red + "blue" :blue + "green" :green + (throw (RuntimeException. (str "color not found: " color))))] + ; append to the map + (assoc acc color-keyword amount))) + {} + colors-string))) + +(defn parse-game + ":: String -> {:id Int :games Vector of {:red Int :green Int :blue Int}} + + Parses a game string into a map with its contents." + [game-string] + (let [game-id-start 5 + game-id-end (.indexOf game-string ":") + game-id-str (.substring game-string game-id-start game-id-end) + game-id (Integer/parseInt game-id-str) + game-rest (.substring game-string (inc game-id-end) (count game-string)) + game-sections (str/split game-rest #";") + game-sections-vec (map parse-single-game game-sections)] + {:id game-id :games game-sections-vec})) + (defn part1 ":: () -> String" [] - "") + (let [count (map parse-game lines)] + (str "count: " count))) (defn part2 ":: () -> String"