From 316785b6b9038ef86571b9cb4f63b2055e6a5400 Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Tue, 5 Dec 2023 02:15:59 -0500 Subject: [PATCH] day 5 --- 2023/src/day05.clj | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 2023/src/day05.clj diff --git a/2023/src/day05.clj b/2023/src/day05.clj new file mode 100644 index 0000000..cc2d540 --- /dev/null +++ b/2023/src/day05.clj @@ -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)) +