From 5366c4c09e946c108b184e4734483bcbb0501164 Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Mon, 4 Dec 2023 01:11:19 -0500 Subject: [PATCH] do day 4 --- 2023/src/day04.clj | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2023/src/day04.clj diff --git a/2023/src/day04.clj b/2023/src/day04.clj new file mode 100644 index 0000000..ddc873b --- /dev/null +++ b/2023/src/day04.clj @@ -0,0 +1,49 @@ +(ns ^{:doc "Day 4" + :author "Adam Jeniski"} + day04 (:require + [clojure.math :as math] + [clojure.set :refer [intersection]] + [clojure.string :as string] + [core :refer [get-puzzle-input]])) + +(def lines (get-puzzle-input 4)) + +(defn parse-nums [nums] + (->> (string/split nums #" ") + (filter not-empty) + (map #(Integer/parseInt %)) + (into #{}))) + +;; part 1 +(->> lines + (map #(re-find #"^Card\s*\d+: ([\s\d]+) \| ([\s\d]+)" %)) + (map rest) + (map (partial map parse-nums)) + (map (partial apply intersection)) + (map count) + (map #(math/pow 2 (dec %))) + (map int) + (reduce +)) + +;; part 2 +(loop [acc 0 + data (->> lines + (map #(re-find #"^Card\s*\d+: ([\s\d]+) \| ([\s\d]+)" %)) + (map rest) + (map (fn [[win actual]] + [1 (count (intersection (parse-nums win) + (parse-nums actual)))])) + (into []))] + (if (not-empty data) + (let [cnt-to-add (second (first data)) + curr-cnt (ffirst data)] + + (recur + (+ acc curr-cnt) + (concat (->> data + rest + (take cnt-to-add) + (map #(update % 0 + curr-cnt))) + (drop (inc cnt-to-add) data)))) + acc)) +