mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
golf
This commit is contained in:
parent
3bfcd41f4f
commit
898532fd6d
@ -5,7 +5,9 @@
|
|||||||
(def input-cache (atom {}))
|
(def input-cache (atom {}))
|
||||||
(def cookie (str "session=" (slurp "session")))
|
(def cookie (str "session=" (slurp "session")))
|
||||||
|
|
||||||
(defn get-puzzle-input [day]
|
(defn get-puzzle-input
|
||||||
|
"retrieves and caches aoc input for 2023 given a day"
|
||||||
|
[day]
|
||||||
(or (@input-cache day)
|
(or (@input-cache day)
|
||||||
(swap! input-cache assoc day
|
(swap! input-cache assoc day
|
||||||
(-> (str "https://adventofcode.com/2023/day/" day "/input")
|
(-> (str "https://adventofcode.com/2023/day/" day "/input")
|
||||||
@ -14,12 +16,36 @@
|
|||||||
:body
|
:body
|
||||||
string/split-lines))))
|
string/split-lines))))
|
||||||
|
|
||||||
(defn re-seq-pos [pattern string]
|
(defn re-seq-pos
|
||||||
|
"re-seq that produces a seq of {:start :end :group}"
|
||||||
|
[pattern string]
|
||||||
(let [m (re-matcher pattern string)]
|
(let [m (re-matcher pattern string)]
|
||||||
((fn step []
|
((fn step []
|
||||||
(when (. m find)
|
(when (. m find)
|
||||||
(cons {:start (. m start) :end (. m end) :group (. m group)}
|
(cons {:start (. m start) :end (. m end) :group (. m group)}
|
||||||
(lazy-seq (step))))))))
|
(lazy-seq (step))))))))
|
||||||
|
|
||||||
|
(defn split-spaces [s]
|
||||||
|
(string/split s #" "))
|
||||||
|
|
||||||
|
(defmacro comp>
|
||||||
|
"comp, but fn-args are applied from left to right"
|
||||||
|
[& ls] (let [r (reverse ls)]
|
||||||
|
`(comp ~@r)))
|
||||||
|
|
||||||
|
(defmacro static-fn
|
||||||
|
"wraps java static methods in a lambda so they can be passed as function objects"
|
||||||
|
[f] `#(~f %))
|
||||||
|
|
||||||
|
(defn mapvf
|
||||||
|
"returns a function that does mapv over f when called with a coll"
|
||||||
|
[f] #(mapv f %))
|
||||||
|
|
||||||
|
(defn reducef
|
||||||
|
"returns a function that reduces over f when called with a coll"
|
||||||
|
([f] #(reduce f %))
|
||||||
|
([f init] #(reduce f init %)))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
|
(map (static-fn Long/parseLong) ["123" "456"])
|
||||||
input-cache)
|
input-cache)
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
(ns day09 (:require [core :refer [get-puzzle-input]]
|
(ns day09 (:require
|
||||||
[clojure.string :as str]))
|
[core :refer [comp> get-puzzle-input mapvf reducef split-spaces]]))
|
||||||
|
|
||||||
(defn parse-line [line] (->> (str/split line #" ")
|
|
||||||
(mapv #(Long/parseLong %))))
|
|
||||||
|
|
||||||
(defn generate-pyramid [nums]
|
(defn generate-pyramid [nums]
|
||||||
(loop [pyramid [nums]]
|
(loop [pyramid [nums]]
|
||||||
@ -11,10 +8,10 @@
|
|||||||
pyramid
|
pyramid
|
||||||
(recur (conj pyramid (mapv - (rest last-line) last-line)))))))
|
(recur (conj pyramid (mapv - (rest last-line) last-line)))))))
|
||||||
|
|
||||||
(defn solve [reduction] (->> (get-puzzle-input 9)
|
(defn solve [reduction]
|
||||||
(map (comp reverse generate-pyramid parse-line))
|
(reduce + (map (comp> split-spaces (mapvf parse-long) generate-pyramid reverse
|
||||||
(map #(reduce reduction 0 %))
|
(reducef reduction 0))
|
||||||
(reduce +)))
|
(get-puzzle-input 9))))
|
||||||
|
|
||||||
;; part 1
|
;; part 1
|
||||||
(solve (fn [num line] (+ (peek line) num)))
|
(solve (fn [num line] (+ (peek line) num)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user