This commit is contained in:
Aleh Suprunovich 2025-12-01 18:17:22 +03:00
commit a180f1d14c
7 changed files with 190 additions and 0 deletions

18
.gitignore vendored Normal file
View File

@ -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/

7
README.md Normal file
View File

@ -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

0
cookies/.keep Normal file
View File

3
deps.edn Normal file
View File

@ -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"}}}

54
src/day1.clj Normal file
View File

@ -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)))

9
src/helpers.clj Normal file
View File

@ -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)))

99
src/mapped_area.clj Normal file
View File

@ -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)