diff options
author | Aria <me@aria.rip> | 2023-01-02 21:58:56 +0000 |
---|---|---|
committer | Aria <me@aria.rip> | 2023-01-02 21:58:56 +0000 |
commit | 5eb58ad076f2cd435b11b140820da224b60b73d5 (patch) | |
tree | 2a67939595fbf993ff04f69b9cd3f0aa20827d96 /2021/day6/06.clj |
initial commit
Diffstat (limited to '2021/day6/06.clj')
-rw-r--r-- | 2021/day6/06.clj | 30 |
1 files changed, 30 insertions, 0 deletions
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) |