aboutsummaryrefslogtreecommitdiff
path: root/2021/day6
diff options
context:
space:
mode:
Diffstat (limited to '2021/day6')
-rw-r--r--2021/day6/06.clj30
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)