Skip to content

Instantly share code, notes, and snippets.

@galuque
Created December 6, 2021 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galuque/25e5adf15976ea36023cf69af43c7310 to your computer and use it in GitHub Desktop.
Save galuque/25e5adf15976ea36023cf69af43c7310 to your computer and use it in GitHub Desktop.
(ns io.github.galuque.aoc-2021.day-6
(:require [clojure.java.io :as io]
[clojure.string :as str]))
(def input (->> "day_6/input.txt"
io/resource
slurp))
(def sample-input "3,4,3,1,2")
(def init-ages (into [] (repeat 9 0)))
(defn ages-vec [coll [v f]]
(update coll v + f))
(defn parse-input [input]
(->> (str/split (str/trim input) #",")
(map parse-long)
frequencies
(reduce ages-vec init-ages)))
(defn day-passed [ages]
(reduce conj [(subvec ages 1 7)
(+ (nth ages 7) (nth ages 0))
(nth ages 8)
(nth ages 0)]))
(defn count-fish [days ages]
(->> ages
(iterate day-passed)
(take (inc days))
last
(apply +)))
(def part-1 (partial count-fish 80))
(comment
(part-1 (parse-input sample-input))
;; => 5934
(part-1 (parse-input input))
;; => 374994
)
(def part-2 (partial count-fish 256))
(comment
(part-2 (parse-input sample-input))
;; => 26984457539
(part-2 (parse-input input))
;; => 1686252324092
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment