From 505453974ba87a742ad9cecebe4d35fbd0d2b127 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 13 May 2026 07:39:51 -0400 Subject: [PATCH] refactor day 03 --- 2024/src/day03.clj | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/2024/src/day03.clj b/2024/src/day03.clj index a31f27c..132189c 100644 --- a/2024/src/day03.clj +++ b/2024/src/day03.clj @@ -1,37 +1,31 @@ (ns day03 - (:require [core :as c] - [input-manager :refer [get-input]] + (:require [input-manager] [clojure.string :as str])) -(def input (str/join (get-input 3))) +(def input (input-manager/get-input-raw 2024 3)) -(defn parse-muls - "takes in a string containing mul instructions - returns list of parsed integral multiplication results" - [s] - (->> (c/get-match-groups #"mul\((\d{1,3}),(\d{1,3})\)" s) - (map #(map parse-long %)) - (map #(reduce * %)))) +(defn compute-muls [s] + (map (comp #(reduce * %) + #(map parse-long %) + #(str/split % #",") + #(subs % 4 (dec (count %)))) ;; remove `mul(` prefix and `)` suffix + s)) + +(defn enforce-toggles [[el & rst]] + (cond (= el "do()") (recur rst) + (= el "don't()") (recur (drop-while #(not= % "do()") rst)) + el (cons el (lazy-seq (enforce-toggles rst))))) ;; part 1 (->> input - parse-muls + (re-seq #"mul\(\d{1,3},\d{1,3}\)") + compute-muls (reduce +)) ;; part 2 (->> input - (c/re-pos #"mul\((\d{1,3}),(\d{1,3})\)|(do\(\))|(don't\(\))") - (sort-by key) - (reduce - (fn [acc [_idx instr]] - (cond - (.startsWith instr "mul") (if (:on acc) - (update acc :val - + (->> instr parse-muls first)) - acc) - (.startsWith instr "don't") (assoc acc :on false) - (.startsWith instr "do") (assoc acc :on true))) - {:on true - :val 0}) - :val) + (re-seq #"mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\)") + enforce-toggles + compute-muls + (reduce +))