From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2021/day6/06.clj | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2021/day6/06.clj (limited to '2021/day6/06.clj') diff --git a/2021/day6/06.clj b/2021/day6/06.clj new file mode 100644 index 0000000..9531425 --- /dev/null +++ b/2021/day6/06.clj @@ -0,0 +1,30 @@ +(ns day-6) + +(require '[clojure.string :as str]) + +(defn add-fish [h days count] + (assoc h days (+ count (or (h days) 0)))) + +(defn simulate-fish [h [days count]] + (if (= 0 days) (-> (add-fish h 6 count) + (add-fish 8 count)) + (add-fish h (- days 1) count))) + +(defn simulate-fishes [h] + (reduce simulate-fish {} h)) + +(def input (as-> (slurp "./input") x + (str/split x #",") + (map str/trim x) + (map #(. Integer parseInt %) x))) + +(def inputMap (reduce (fn [acc days] (add-fish acc days 1)) {} input)) + +(def resultA (reduce (fn [acc _] (simulate-fishes acc)) inputMap (range 80))) +(def resultB (reduce (fn [acc _] (simulate-fishes acc)) inputMap (range 256))) + +(def countA (reduce (fn [acc [_ count]] (+ acc count)) 0 resultA)) +(def countB (reduce (fn [acc [_ count]] (+ acc count)) 0 resultB)) + +(println countA) +(println countB) -- cgit v1.2.3