Fork me on GitHub
#meander
<
2023-12-14
>
nuriaion09:12:44

Hi I'm using Advent of Code to learn Clojure and meander. In the i'm trying to parse the input of day 2 and get the following:

[:S
 [:game
  [:id "1"]
  [:draws
   [:draw
    [:cubes "3" "blue" "4" "red"]
    [:cubes "1" "red" "2" "green" "6" "blue"]
    [:cubes "2" "green"]]]]]
which i want to transform to:
{:id "1",
 :draws
 [[{:number "3", :color "blue"}
   {:number "3", :color "blue"}]
  [{:number "1", :color "red"}
   {:number "2", :color "green"}
   {:number "6", :color "blue"}]
  [{:number "2", :color "green"}]]}
So far i tried with:
(m/rewrites parsed
      [:S
       [:game
        [:id ?id]
        [:draws
         [:draw [:cubes . !n !c ..1] ..1 & ?rest]]]]
      {:id ?id
       :draws [{:draw [{:number !n :color !c}]} ...]
       :rest ?rest})))
which result in:
({:id "1",
  :draws
  [{:draw [{:number "3", :color "blue"}]}
   {:draw [{:number "4", :color "red"}]}],
  :rest
  [[:cubes "1" "red" "2" "green" "6" "blue"] [:cubes "2" "green"]]})
but i have the big problem to get this 2 levels of nesting. Can anyone give me tip in which direction i should look?

noprompt22:12:13

I know this is 12 days later (I've been too busy with work took keep with the channel) but my recommendation is to break things like this down in to smaller rules and use m/cata to rewrite the structure recursively:

(m/rewrite parsed
  [:S
   [:game
    [:id ?id]
    [:draws
     [:draw & [(m/cata !draws) ...]]]]]
  {:id ?id :draws [!draws ...]}

  [:cubes & [!number !color ...]]
  [{:number !number, :color !color} ...])

nuriaion08:01:56

Thanks for your replay, in the end i also found cata. Nice to see that's the way to go 🙂