This commit is contained in:
Adam Jeniski 2023-12-05 02:15:59 -05:00
parent 05e888539b
commit 316785b6b9

58
2023/src/day05.clj Normal file
View File

@ -0,0 +1,58 @@
(ns ^{:doc "Day 5: If You Give A Seed A Fertilizer"
:author "Adam Jeniski"}
day05 (:require [clojure.string :as string]
[core :refer [get-puzzle-input]]))
(def input (get-puzzle-input 5))
(def seeds (->> (first input)
(#(string/split % #" "))
(rest)
(map #(Long/parseLong %))))
(def rates (->> input
(partition-by empty?)
(filter (partial some not-empty))
rest
(map rest)
(map (fn [nums] (->> nums
(map #(string/split % #" "))
(map (partial map #(Long/parseLong %)))
(sort-by second))))))
(defn find-rate [rate-list curr]
(loop [lines rate-list]
(if (not-empty lines)
(let [[dest source cnt] (first lines)]
(if (<= source curr (dec (+ source cnt)))
(+ (- dest source) curr)
(recur (rest lines))))
curr)))
(defn translate [seed]
(loop [curr seed
rate-lists rates]
(if (not-empty rate-lists)
(recur (find-rate (first rate-lists) curr)
(rest rate-lists))
curr)))
;; part 1
(->> seeds
(map translate)
(apply min))
;; part 2
(->> seeds
(partition 2)
(map #(range (+
118 ;; hacky solution, delete this + 117
(first %))
(+ (first %)
(second %))
1000 ;; hacky solution, delete this
))
(apply concat)
(map translate)
(apply min))