Advent of Code 2025 Day 2
Day 2 of solving The Advent of Code in Clojure .
The 2nd day had us do some magic with numbers. The quest was to find some silly patterns in the numbers. I chose to use good old regular expressions to do the job, while a colleague of mine chose to use math. Both work really well. The 2nd part of the puzzle is to find repeating patterns, where the 1st only wanted pairs.
clojure code snippet start
(ns day2
(:require
[clojure.string :as str]))
(def parse-int Long/parseLong)
(defn parse-ranges [input]
(map (fn [in]
(let [[_ start end] (first (re-seq #"(\d+)-(\d+)" in))]
(filter #(re-matches #"(\d+)\1" (str %))
(range (parse-int start) (inc (parse-int end))))))
input))
(defn parse-ranges2 [input]
(map (fn [in]
(let [[_ start end] (first (re-seq #"(\d+)-(\d+)" in))]
(filter #(re-matches #"(\d+)(\1+)" (str %))
(range (parse-int start) (inc (parse-int end))))))
input))
(def part1 (reduce +
(->
(slurp "resources/two.in")
(str/trim)
(str/split #",")
(parse-ranges)
(flatten))))
(def part2 (reduce + (->
(slurp "resources/two.in")
(str/trim)
(str/split #",")
(parse-ranges2)
(flatten))))
(prn part1 part2)clojure code snippet end