commit a180f1d14c98911d02fc45327596bc498addf6f0 Author: Aleh Suprunovich Date: Mon Dec 1 18:17:22 2025 +0300 day 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dfe7865 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +cookies/session + +# ---> Clojure +pom.xml +pom.xml.asc +*.jar +*.class +/lib/ +/classes/ +/target/ +/checkouts/ +.lein-deps-sum +.lein-repl-history +.lein-plugins/ +.lein-failures +.nrepl-port +.cpcache/ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..24b575e --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# advent-of-code-2024 + +To get puzzle inputs you need to place your session cookie to `cookies/session`. + +To run: + + clj -M src/dayX.clj diff --git a/cookies/.keep b/cookies/.keep new file mode 100644 index 0000000..e69de29 diff --git a/deps.edn b/deps.edn new file mode 100644 index 0000000..ab3bf55 --- /dev/null +++ b/deps.edn @@ -0,0 +1,3 @@ +{:paths ["src" "cookies"] + :deps {org.clojure/clojure {:mvn/version "1.12.3"} + clj-http/clj-http {:mvn/version "3.13.0"}}} diff --git a/src/day1.clj b/src/day1.clj new file mode 100644 index 0000000..37368cd --- /dev/null +++ b/src/day1.clj @@ -0,0 +1,54 @@ +(ns day1 + (:require [clojure.string :as string] + [helpers :refer [get-input]])) + +(def test-input "L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 +") + +(defn- parse-line [l] + [(first l) (Integer/parseInt (apply str (rest l)))]) + +(defn- parse-input [i] + (->> (string/split-lines i) + (map parse-line))) + +(defn- rotate [values [direction step]] + (let [value (peek values)] + (conj + values + (if (= direction \R) + (rem (+ value step) 100) + (let [value (- value (rem step 100))] + (if (neg? value) + (- 100 (rem (- value) 100)) + value)))))) + +;(def input (parse-input test-input)) +(def input (parse-input (get-input 1))) + +(def dial (reduce rotate [50] input)) + +(println "Password:" (count (filterv zero? dial))) + +(def rotations (partition 2 (interleave dial input))) + +(defn- count-zeros [[a [direction step]]] + (cond + (= direction \R) + (quot (+ a step) 100) + (= direction \L) + (if (>= step a) + (let [r (inc (abs (quot (- a step) 100)))] + (if (zero? a) (dec r) r)) + 0))) + +(println "Password method 0x434C49434B:" (apply + (map count-zeros rotations))) diff --git a/src/helpers.clj b/src/helpers.clj new file mode 100644 index 0000000..062b2b5 --- /dev/null +++ b/src/helpers.clj @@ -0,0 +1,9 @@ +(ns helpers + (:require [clj-http.client :as client])) + +(defn get-input [day] + (let [url (format "https://adventofcode.com/2025/day/%d/input" day) + session-cookie (slurp "cookies/session")] + (->> + (client/get url {:headers {"Cookie" session-cookie}}) + :body))) diff --git a/src/mapped_area.clj b/src/mapped_area.clj new file mode 100644 index 0000000..09a7e02 --- /dev/null +++ b/src/mapped_area.clj @@ -0,0 +1,99 @@ +(ns mapped-area + (:require [clojure.string :as string])) + +(def test-map +"abcd +efgh +ijkl") + +(defn read-mapped-area + {:test + #(do + (let [a (read-mapped-area test-map)] + (assert (= \i (get-in a [0 0]))) + (assert (= \f (get-in a [1 1]))) + (assert (= \a (get-in a [0 2]))) + (assert (nil? (get-in a [10 10]))) + (assert (nil? (get-in a [1 3]))) + (assert (= \h (get-in a [3 1])))))} + [s] + (let [ss (string/split-lines s) + lines (count ss)] + (->> + (reverse ss) + (apply interleave) + (partition lines) + (mapv vec)))) + +(defn print-mapped-area [a] + (let [lines (count (first a))] + (doall + (->> + (map reverse a) + (apply interleave) + (partition lines) + (map (partial apply str)) + (map println)))) + a) + +(comment + (print-mapped-area (read-mapped-area test-map)) + ) + +(defn- h + {:test #(do (assert (= (h (read-mapped-area test-map)) 3)))} + [a] + (count (first a))) + +(defn- w + {:test #(do (assert (= (w (read-mapped-area test-map)) 4)))} + [a] + (count a)) + +(defn mapped? + {:test + #(do + (let [a (read-mapped-area test-map)] + (assert (true? (mapped? a 1 1))) + (assert (true? (mapped? a [2 2]))) + (assert (false? (mapped? nil -1 0))) + (assert (false? (mapped? a 4 0))) + (assert (true? (mapped? a 3 1))) + (assert (false? (mapped? a 1 3)))))} + ([a [x y]] (mapped? a x y)) + ([a x y] + (and (>= x 0) + (>= y 0) + (< x (w a)) + (< y (h a))))) + +(defn mapped-area-indexed-seq [a] + (for [x (range (w a)) + y (range (h a))] + (vector [x y] (get-in a [x y])))) + +(def test-map-num +"1234 +5678") + +(defn parse-elements + {:test + #(do + (let [a (parse-elements + (read-mapped-area test-map-num))] + (assert (number? (get-in a [0 0]))) + (assert (== 5 (get-in a [0 0]))) + (assert (== 1 (get-in a [0 1])))))} + ([a] (parse-elements a (comp parse-long str))) + ([a parse-fn] + (reduce + (fn [coll [coord value]] + (assoc-in coll coord (parse-fn value))) + [[]] + (mapped-area-indexed-seq a)))) + +(test #'read-mapped-area) +(test #'h) +(test #'w) +(test #'mapped?) +(test #'parse-elements)