blob: 953142532abce8f873df72bfea1938da333b8869 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)
|