I've been trying Advent of Code challenges to learn Clojure. I've successfully completed Day 2, but I feel like my solution is very clumsy, especially the functions for parsing text input.
Instructions:
Imagine there is a sequence of games played, encoded in a text file, one game per line. Each game consists of an id and a sequence of (semicolon-delimited) draws of colored cubes from a small bag. Each draw is encoded as a (comma-delimited) sequence of numbers of cubes drawn per color.
Example line (game):
Game 2: 1 green, 1 blue, 1 red; 11 red, 3 blue; 1 blue, 18 red; 9 red, 1 green; 2 blue, 11 red, 1 green; 1 green, 2 blue, 10 red
The goal is to parse such file for further processing.
My solution:
(defn parse-coloring [coloring]
(let [tokens (clojure.string/split coloring #" ")
color (second tokens)
value (Integer/parseInt (first tokens))]
{ :color color :value value }))
(defn parse-draw [draw]
(let [colorings (clojure.string/split draw #", ")]
(map parse-coloring colorings)))
(defn parse-line [line]
(let [matches (re-matches #"^Game (\d+): (.*)$" line)
game-id (Integer/parseInt (nth matches 1))
game (nth matches 2)
draws (clojure.string/split game #"; ")]
{ :id game-id :draws (map parse-draw draws)}))
(def games
(let [input (slurp "C:\\Users\\...\\input.txt")
lines (clojure.string/split-lines input)]
(map parse-line lines)))
I'm looking for a more simple, succinct, idiomatic-clojure solution.