Guest User

advent-of-code-2025-day1-clj

a guest
Dec 20th, 2025
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Clojure 1.20 KB | Source Code | 0 0
  1. (ns advent.core
  2.   (:require [clojure.java.io :as io]))
  3.  
  4. (defn read-input []
  5.   (with-open [rdr (io/reader "input01.txt")]
  6.     (into [] (line-seq rdr))))
  7.  
  8. (defn parsed-rotations [rotations]
  9.   (mapv (fn [r]
  10.           (let [n (Integer/parseInt (subs r 1))]
  11.             (case (first r)
  12.               \L (- n)
  13.               \R n)))
  14.         rotations))
  15.  
  16. ;; PART1
  17. (second
  18.  (reduce (fn [[position counter] r]
  19.            (let [position' (mod (+ position r) 100)]
  20.              (if (zero? position')
  21.                [position' (inc counter)]
  22.                [position' counter])))
  23.          [50 0]
  24.          (parsed-rotations (read-input "input01.txt"))))
  25.  
  26. ;; PART2
  27. (second
  28.  (reduce (fn [[position counter] r]
  29.            (let [position' (mod (+ position r) 100)
  30.                  turns     (quot (abs r) 100)
  31.                  counter'  (+ counter turns)]
  32.              (if (or (zero? position')
  33.                      (and (not (zero? position)) (neg? r) (> position' position))
  34.                      (and (not (zero? position)) (pos? r) (< position' position)))
  35.                [position' (inc counter')]
  36.                [position' counter'])))
  37.          [50 0]
  38.          (parsed-rotations (read-input "input01.txt"))))
Advertisement
Add Comment
Please, Sign In to add comment