WIP; do day 25; todo: fix imports for previous days

This commit is contained in:
Adam Jeniski 2024-12-25 12:12:03 -05:00
parent bef47f68b7
commit ce9969a9df
5 changed files with 37 additions and 144 deletions

View File

@ -1,4 +1,4 @@
{:deps {org.clojure/clojure {:mvn/version "1.12.0"} {:deps {org.clojure/clojure {:mvn/version "1.12.0"}
org.clojure/math.combinatorics {:mvn/version "0.3.0"}} org.clojure/math.combinatorics {:mvn/version "0.3.0"}}
:paths ["src"] :paths ["src" "../shared/clj/src"]
:tasks {}} :tasks {}}

View File

@ -2,7 +2,9 @@
:description "my 2024 advent of code solutions in clojure" :description "my 2024 advent of code solutions in clojure"
:url "http://github.com/ajetski/advent-of-code" :url "http://github.com/ajetski/advent-of-code"
:min-lein-version "2.0.0" :min-lein-version "2.0.0"
:jvm-opts ["-Xmx16g"]
:dependencies [[org.clojure/clojure "1.12.0"] :dependencies [[org.clojure/clojure "1.12.0"]
[org.clojure/math.combinatorics "0.3.0"] [org.clojure/math.combinatorics "0.3.0"]
[babashka/fs "0.5.23"] [babashka/fs "0.5.23"]
[org.babashka/http-client "0.4.22"]]) [org.babashka/http-client "0.4.22"]]
:source-paths ["src" "../shared/clj/src"])

View File

@ -1,117 +0,0 @@
(ns core
(:require
[clojure.string :as str]))
;; string/regex stuff
(defn split-whitespace [s]
(str/split s #"\s+"))
(defn split-on-double-newlines [s]
(str/split s #"\n\n"))
(defn get-match-groups [regex s]
(->> s (re-seq regex) (map rest)))
(defn re-pos [re s]
(loop [m (re-matcher re s)
res {}]
(if (.find m)
(recur m (assoc res (.start m) (.group m)))
res)))
;; general utils
(defn dbg [x]
(println x)
x)
(defn log [msg]
(spit "logs.txt" msg :append true))
(defn compose [& fs]
(apply comp (reverse fs)))
(defn bool->binary [condition]
(if condition 1 0))
;; alter collections
(defn get-coords [list-of-lists]
(for [row (range (count list-of-lists))
col (range (count (get list-of-lists row)))]
[row col]))
(defn map-by-coords [arr-2d]
(->> arr-2d
get-coords
(map (juxt identity #(get (get arr-2d (first %)) (second %))))
(into {})))
(defn insert-at-idx [coll idx el]
(concat (take idx coll)
(list el)
(drop idx coll)))
(defn mmap
"map map f coll"
[f & colls]
(apply map (partial map f) colls))
(defn mmapv
"mapv mapv f coll"
[f & colls]
(apply mapv (partial mapv f) colls))
(defn mmmap
"map map map f coll"
[f & colls]
(apply map (partial map (partial map f)) colls))
(defn mmmapv
"mapv mapv mapv f coll"
[f & colls]
(apply mapv (partial mapv (partial mapv f)) colls))
(defn partition-by-counts [counts coll]
(->> counts
(reduce (fn [[acc coll] c]
(let [[a b] (split-at c coll)]
[(conj acc a) b]))
[[] coll])
first))
(defn update-last [v f & args]
(let [idx (dec (count v))]
(apply update v idx f args)))
(defn partition-by-range-gap [sorted-nums]
(:acc (reduce (fn [{acc :acc
a :last}
el]
(if (= el (inc a))
{:acc (update-last acc conj el)
:last el}
{:acc (conj acc [el])
:last el}))
{:acc [[(first sorted-nums)]]
:last (first sorted-nums)}
(rest sorted-nums))))
;; Math things
(defn square [n] (* n n))
(defn mean [a] (/ (reduce + a) (count a)))
(defn standard-deviation
[a]
(let [mn (mean a)]
(Math/sqrt
(/ (reduce #(+ %1 (square (- %2 mn))) 0 a)
(dec (count a))))))
(def arrow-char->dir {\> :right
\v :down
\< :left
\^ :up})

33
2024/src/day25.clj Normal file
View File

@ -0,0 +1,33 @@
(ns day25
(:require
[clojure.string :as s]
[core :refer :all :as c]
[input-manager :refer :all]))
(def input (->> (split-on-double-newlines (get-input-raw 2024 25))
(map s/split-lines)))
(defn get-counts [str-rows]
(->> (apply map vector str-rows) ;; flip grid over y=x axis
(map (partial filter #(= \# %)))
(map count)))
(def key-counts (->> input
(filter #(every? (partial = \.) (first %)))
(map get-counts)))
(def lock-counts (->> input
(filter #(every? (partial = \#) (first %)))
(map get-counts)))
(def space (count (first input)))
;; part 1
(->> (for [k key-counts
l lock-counts]
(map + k l))
(filter (partial every? #(<= % space)))
count)
;; classic, no part 2. just get the rest of the stars!

View File

@ -1,25 +0,0 @@
(ns input-manager
(:require
[babashka.fs :as fs]
[babashka.http-client :as http]
[clojure.string :as str]))
(def session (first (fs/read-all-lines "input/.session")))
(defn input-file-loc [day]
(str "input/" day ".txt"))
(defn download-input [day]
(->> {:headers {:cookie (str "session=" session)}}
(http/get (str "https://adventofcode.com/2024/day/" day "/input"))
:body
(str/split-lines)
(fs/write-lines (input-file-loc day))))
(defn get-input [day]
(try
(fs/read-all-lines (input-file-loc day))
(catch java.nio.file.NoSuchFileException _e
(download-input day)
(get-input day))))