-
Notifications
You must be signed in to change notification settings - Fork 6
/
day06.clj
34 lines (27 loc) · 910 Bytes
/
day06.clj
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
31
32
33
34
(ns advent-2021-clojure.day06
(:require [advent-2021-clojure.utils :refer [parse-int]]))
(def delivery-timer 0)
(def post-delivery-timer 6)
(def new-fish-timer 8)
(def no-fish {0 0, 1 0, 2 0, 3 0, 4 0, 5 0, 6 0, 7 0, 8 0})
(defn parse-fish [input]
(->> (re-seq #"\d" input)
(map parse-int)
frequencies
(merge no-fish)))
(defn next-timer [timer]
(if (= timer delivery-timer) post-delivery-timer (dec timer)))
(defn next-generation [fish]
(let [deliveries (fish delivery-timer)]
(-> (reduce-kv (fn [m k v] (update m (next-timer k) + v)) no-fish fish)
(assoc new-fish-timer deliveries))))
(defn nth-generation [n fish]
(-> (iterate next-generation fish)
(nth n)))
(defn solve [input num-days]
(->> (parse-fish input)
(nth-generation num-days)
vals
(apply +)))
(defn day1 [input] (solve input 80))
(defn day2 [input] (solve input 256))