From f91fe68f6bfb152e0cca143850dcd35b9c72d5dc Mon Sep 17 00:00:00 2001 From: Aleh Suprunovich Date: Tue, 2 Dec 2025 15:27:06 +0300 Subject: [PATCH] day 2 --- src/day2.clj | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/day2.clj diff --git a/src/day2.clj b/src/day2.clj new file mode 100644 index 0000000..55ea8d7 --- /dev/null +++ b/src/day2.clj @@ -0,0 +1,52 @@ +(ns day2 + (:require [clojure.edn :as edn] + [clojure.string :as string] + [helpers :refer [get-input]])) + + +(def test-input "11-22,95-115,998-1012,1188511880-1188511890,222220-222224, +1698522-1698528,446443-446449,38593856-38593862,565653-565659, +824824821-824824827,2121212118-2121212124") + +(defn- gen-range [spec] + (let [[a b] (string/split (string/trim spec) #"-") + a (edn/read-string a) + b (inc (edn/read-string b))] + (map str (range a b)))) + +(defn- invalid? [id] + (let [c (count id)] + (and (even? c) + (let [first-half (apply str (take (/ c 2) id))] + (string/ends-with? id first-half))))) + +;(def input (string/split test-input #",")) +(def input (string/split (get-input 2) #",")) + +(println + "Sum of all invalid IDs:" + (->> + input + (mapcat gen-range) + (filter invalid?) + (map edn/read-string) + (reduce +))) + +(defn- invalid?' [id] + (if (= 1 (count id)) + false + (let [h (inc (/ (count id) 2)) + make-ptn (fn [n] + (re-pattern + (str "(" (apply str (take n id)) ")+"))) + patterns (map make-ptn (range 1 h))] + (some #(re-matches % id) patterns)))) + +(println + "Sum of all invalid IDs (part 2):" + (->> + input + (mapcat gen-range) + (filter invalid?') + (map edn/read-string) + (reduce +)))