Compare commits

...

12 Commits

Author SHA1 Message Date
Adam Jeniski
e4480066b7 do day 3 2026-06-01 11:12:05 -04:00
56738fd7f9 Merge branch 'master' of git.ajet.fyi:ajet/advent-of-code 2026-05-15 09:39:52 -04:00
Adam Jeniski
b89c7ce3f2 do day 13 2026-05-15 09:27:49 -04:00
Adam Jeniski
a1db8ae128 do day 12 2026-05-15 09:27:46 -04:00
Adam Jeniski
e6357d9802 do day 11 2026-05-15 09:27:43 -04:00
b62c9346f5 cons is for boomers 2026-05-13 07:55:24 -04:00
505453974b refactor day 03 2026-05-13 07:39:51 -04:00
0aed472a32 do 2017 day 9 2026-05-04 08:47:30 -04:00
Adam Jeniski
9703c017a9 do 2017 day 8 2026-05-04 07:02:38 -04:00
Adam Jeniski
733c833f66 do 2017 day 7 2026-05-04 07:02:34 -04:00
1087a7649b do 2017 day 6 2026-04-26 11:01:02 -04:00
5077215205 do 2017 day 5 2026-04-26 11:00:57 -04:00
10 changed files with 392 additions and 25 deletions
+51
View File
@@ -0,0 +1,51 @@
(ns day03
(:require input-manager))
(def input (parse-long (input-manager/get-input-raw 2017 3)))
(def directions [:right :up :left :down])
(def to-the-left {:right :up
:up :left
:down :right
:left :down})
(def cardinal-dir-offsets {:right [1 0]
:left [-1 0]
:down [0 -1]
:up [0 1]})
(def all-offsets #{[1 0] [-1 0] [0 -1] [0 1] [1 1] [-1 -1] [1 -1] [-1 1]})
(->> (loop [loc [1 0]
i 2
grid {[0 0] 1}
[dir :as dirs] (cycle directions)]
(if (< i input)
(let [left-loc (mapv + loc (cardinal-dir-offsets (to-the-left dir)))
grid' (assoc grid loc i)
i' (inc i)]
(if (contains? grid left-loc)
(recur (mapv + loc (cardinal-dir-offsets dir)) i' grid' dirs)
(recur left-loc i' grid' (rest dirs))))
loc))
(map abs)
(apply +))
(->> (loop [loc [1 0]
grid {[0 0] 1}
[dir :as dirs] (cycle directions)]
(let [left-loc (mapv + loc (cardinal-dir-offsets (to-the-left dir)))
value (->> all-offsets
(map (comp
#(or (grid %) 0)
#(mapv + % loc)))
(reduce +))
grid' (assoc grid loc value)]
(cond
(> value input)
value
(contains? grid left-loc)
(recur (mapv + loc (cardinal-dir-offsets dir)) grid' dirs)
:else
(recur left-loc grid' (rest dirs))))))
+30
View File
@@ -0,0 +1,30 @@
(ns day05
(:require input-manager))
(def input (->> (input-manager/get-input 2017 5)
(mapv parse-long)))
(def MAX_IDX (count input))
;; part 1
(loop [steps 0
maze input
idx 0]
(let [maze' (update maze idx inc)
idx' (+ (maze idx) idx)]
(if (< -1 idx' MAX_IDX)
(recur (inc steps) maze' idx')
(inc steps))))
;; part 2
(loop [steps 0
maze input
idx 0]
(let [offset (maze idx)
maze' (update maze idx (if (>= offset 3)
dec
inc))
idx' (+ offset idx)]
(if (< -1 idx' MAX_IDX)
(recur (inc steps) maze' idx')
(inc steps))))
+26
View File
@@ -0,0 +1,26 @@
(ns day06
(:require [input-manager]
[core :refer [split-whitespace]]))
(def input
(->> (input-manager/get-input-raw 2017 6)
split-whitespace
(mapv parse-long)))
(loop [state input
i 0
seen {}]
(if (contains? seen state)
{:part-one i
:part-two (- i (seen state))}
(let [cnt (apply max state)
idx (->> state
(map-indexed vector)
(filter #(= (second %) cnt))
ffirst)]
(recur (->> (range cnt)
(map #(mod (+ idx 1 %) (count state)))
(reduce #(update %1 %2 inc) (assoc state idx 0)))
(inc i)
(assoc seen state i)))))
+67
View File
@@ -0,0 +1,67 @@
(ns day07
(:require
[clojure.string :as str]
[core :refer [split-whitespace]]
[input-manager]))
(def input
(->> (input-manager/get-input-raw 2017 7)
str/split-lines
(map (comp split-whitespace
#(str/replace % #"," "")))))
;; part 1
(def base-node
(->> input
(filter (fn [[_ _ _ & children]] children))
(filter (fn [[node]]
(empty? (filter (fn [[_ _ _ & children]]
(some #(= % node) children))
input))))
ffirst))
(do
(def get-depth
(memoize (fn [node]
(let [[_ w-str _ & children] (first (filter (fn [[n]] (= node n))
input))
w (parse-long (subs w-str 1 (dec (count w-str))))]
(apply + w (map get-depth children))))))
(def get-weight
(memoize (fn [node]
(let [w-str (->> input
(filter (fn [[n]] (= node n)))
first
second)]
(parse-long (subs w-str 1 (dec (count w-str))))))))
(def get-children
(memoize (fn [node] (->> input
(filter (fn [[n]] (= node n)))
first
(drop 3))))))
(defn get-children-data [node]
(map (juxt identity get-depth get-weight) (get-children node)))
(defn get-counts [data]
(->> data
(map second)
frequencies
(sort-by second)))
;; part 2
(let [[bad-parent bad-node]
(loop [parent nil
node base-node]
(let [data (get-children-data node)
counts (get-counts data)
row (first (filter #(= (second %) (ffirst counts)) data))]
(if (= (count counts) 1)
[parent node]
(recur node (first row)))))
diff (->> bad-parent
get-children-data
get-counts
(map first)
(apply -))]
(- (get-weight bad-node) diff))
+46
View File
@@ -0,0 +1,46 @@
(ns day08
(:require
[clojure.string :as str]
[core]
[input-manager]))
(def op {"inc" +
"dec" -})
(def cond-op {"<" <
"<=" <=
">" >
">=" >=
"==" =
"!=" not=})
(def input (map (comp
(fn [[name operation num _ cond-name cond-operation cond-val]]
[name
(op operation)
(parse-long num)
cond-name
(cond-op cond-operation)
(parse-long cond-val)])
core/split-whitespace)
(input-manager/get-input 2017 8)))
(defn compute [acc [reg reg-op val cond-reg cond-op cond-val]]
(if (cond-op (acc cond-reg 0) cond-val)
(assoc acc reg (reg-op (acc reg 0) val))
acc))
;; part 1
(->> input
(reduce compute {})
(sort-by second)
last
second)
;; part 2
(->> input
(reductions compute {})
(mapcat identity)
set
(sort-by second)
last
second)
+34
View File
@@ -0,0 +1,34 @@
(ns day09
(:require [clojure.string :as str]
core
input-manager))
(def input (input-manager/get-input-raw 2017 9))
(defn sum-recursive-depths [s sum idx depth]
(if (>= idx (count s))
sum
(condp = (.charAt s idx)
\, (recur s sum (inc idx) depth)
\{ (recur s
(+ sum depth)
(inc idx)
(inc depth))
\} (recur s
sum
(inc idx)
(max (dec depth) 0)))))
(defn exlam-canceler [s] (str/replace s #"!." ""))
(def angle-bracket-regex #"<[^>]*>")
(-> input
exlam-canceler
(str/replace angle-bracket-regex "")
(sum-recursive-depths 0 0 1))
(->> input
exlam-canceler
(re-seq angle-bracket-regex)
(map #(- (count %) 2))
(reduce +))
+27
View File
@@ -0,0 +1,27 @@
(ns day11
(:require [core]
input-manager
[clojure.string :as str]))
(def input (-> (input-manager/get-input-raw 2017 11)
(str/split #",")))
(def offsets {"n" [0 1]
"ne" [0.5 0.5]
"se" [0.5 -0.5]
"s" [0 -1]
"sw" [-0.5 -0.5]
"nw" [-0.5 0.5]})
(defn distance-from-origin [pos]
(let [[x y] (map abs pos)]
(+ (* x 2) (- y x))))
(->> input
(map offsets)
(reductions #(map + %1 %2) [0 0])
(map distance-from-origin)
((juxt last (partial apply max)))
(zipmap [:part-one :part-two]))
+34
View File
@@ -0,0 +1,34 @@
(ns day12
(:require
[clojure.string :as str]
core
input-manager
[clojure.set :refer [difference]]))
(def input (->> (input-manager/get-input 2017 12)
(map #(str/replace % #"," ""))
(map core/split-whitespace)
(map (fn [[a _ & rst]]
[(parse-long a) (mapv parse-long rst)]))
(into {})))
(defn group-from-start [start]
(loop [visited #{}
queue [start]]
(if-not (seq queue)
visited
(let [[curr & rst] queue
ns (filter (complement visited) (input curr))]
(recur (into visited ns)
(concat rst ns))))))
;; part one
(count (group-from-start 0))
;; part two
(loop [ks (into #{} (keys input))
i 0]
(if-not (seq ks)
i
(recur (difference ks (group-from-start (first ks)))
(inc i))))
+58
View File
@@ -0,0 +1,58 @@
(ns day13
(:require
[clojure.string :as str]
[core]
[input-manager]))
(def lengths (->> (input-manager/get-input 2017 13)
(map (comp #(mapv parse-long %)
#(str/split % #": ")))
(into {})))
(defn move-sensor [{:keys [idx depth increasing-depth] :as sensor}]
(let [[keep-moving reverse-step] (if increasing-depth [inc dec] [dec inc])
depth' (keep-moving depth)]
(if (or (> depth' (dec (lengths idx)))
(< depth' 0))
(-> sensor
(update :depth reverse-step)
(update :increasing-depth not))
(assoc sensor :depth depth'))))
(def initial-sensors (->> lengths
keys
(map #(hash-map :depth 0
:idx %
:increasing-depth true))))
(defn is-spotted-by-sensors [acc pos]
(some #(when (and (= (:idx %) pos)
(= (:depth %) 0))
%)
(:sensors acc)))
(->> lengths keys (apply max) inc range
(reduce (fn [acc pos]
(let [spotted (is-spotted-by-sensors acc pos)]
(-> acc
(update :sensors (partial map move-sensor))
(update :solution + (if spotted
(* pos (lengths pos))
0)))))
{:sensors initial-sensors
:solution 0})
:solution)
(loop [i 0
start-val initial-sensors]
(if
(= true
(->> lengths keys (apply max) inc range
(reduce (fn [acc pos]
(let [spotted (is-spotted-by-sensors acc pos)]
(if spotted
(reduced true)
(update acc :sensors (partial map move-sensor)))))
{:sensors start-val})))
(recur (inc i) (map move-sensor start-val))
i))
+19 -25
View File
@@ -1,37 +1,31 @@
(ns day03 (ns day03
(:require [core :as c] (:require [input-manager]
[input-manager :refer [get-input]]
[clojure.string :as str])) [clojure.string :as str]))
(def input (str/join (get-input 3))) (def input (input-manager/get-input-raw 2024 3))
(defn parse-muls (defn compute-muls [s]
"takes in a string containing mul instructions (map (comp #(reduce * %)
returns list of parsed integral multiplication results" #(map parse-long %)
[s] #(str/split % #",")
(->> (c/get-match-groups #"mul\((\d{1,3}),(\d{1,3})\)" s) #(subs % 4 (dec (count %)))) ;; remove `mul(` prefix and `)` suffix
(map #(map parse-long %)) s))
(map #(reduce * %))))
(defn enforce-toggles [[el & rst]]
(cond (= el "do()") (recur rst)
(= el "don't()") (recur (drop-while #(not= % "do()") rst))
el (conj (lazy-seq (enforce-toggles rst)) el)))
;; part 1 ;; part 1
(->> input (->> input
parse-muls (re-seq #"mul\(\d{1,3},\d{1,3}\)")
compute-muls
(reduce +)) (reduce +))
;; part 2 ;; part 2
(->> input (->> input
(c/re-pos #"mul\((\d{1,3}),(\d{1,3})\)|(do\(\))|(don't\(\))") (re-seq #"mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\)")
(sort-by key) enforce-toggles
(reduce compute-muls
(fn [acc [_idx instr]] (reduce +))
(cond
(.startsWith instr "mul") (if (:on acc)
(update acc :val
+ (->> instr parse-muls first))
acc)
(.startsWith instr "don't") (assoc acc :on false)
(.startsWith instr "do") (assoc acc :on true)))
{:on true
:val 0})
:val)