feat(day2): parse game string into map
This commit is contained in:
parent
04a861dfb9
commit
9280405f8f
5
input_test/02.txt
Normal file
5
input_test/02.txt
Normal file
@ -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
|
@ -2,10 +2,10 @@
|
|||||||
"Day 02 funtions"
|
"Day 02 funtions"
|
||||||
(:require
|
(:require
|
||||||
[utils]
|
[utils]
|
||||||
[clojure.string]))
|
[clojure.string :as str]))
|
||||||
|
|
||||||
(def contents ":: String" (utils/read-file "01" false))
|
(def contents ":: String" (utils/read-file "02" true))
|
||||||
(def lines ":: Vector of String" (clojure.string/split-lines contents))
|
(def lines ":: Vector of String" (str/split-lines contents))
|
||||||
|
|
||||||
; cubes: red, green, blue
|
; cubes: red, green, blue
|
||||||
; the Elf will reach into the bag, grab a handful of random cubes,
|
; 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
|
; 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
|
; 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 "<number> <color>"
|
||||||
|
(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
|
(defn part1
|
||||||
":: () -> String"
|
":: () -> String"
|
||||||
[]
|
[]
|
||||||
"")
|
(let [count (map parse-game lines)]
|
||||||
|
(str "count: " count)))
|
||||||
|
|
||||||
(defn part2
|
(defn part2
|
||||||
":: () -> String"
|
":: () -> String"
|
||||||
|
Loading…
Reference in New Issue
Block a user