diff options
Diffstat (limited to 'year2021/day6')
-rw-r--r-- | year2021/day6/consteval.cpp | 32 | ||||
-rw-r--r-- | year2021/day6/part1.clj | 20 | ||||
-rw-r--r-- | year2021/day6/part2.clj | 25 |
3 files changed, 77 insertions, 0 deletions
diff --git a/year2021/day6/consteval.cpp b/year2021/day6/consteval.cpp new file mode 100644 index 0000000..f46da89 --- /dev/null +++ b/year2021/day6/consteval.cpp @@ -0,0 +1,32 @@ +#include <algorithm> +#include <cstdint> +#include <numeric> +#include <iostream> + +consteval auto countFish(unsigned int day) +{ + unsigned char input[] = { + //3,4,3,1,2 +#include "in" + }; + + uint64_t counts[9]; + std::fill(counts, counts + 9, 0); + for (int i = 0; i < sizeof(input); ++i) + ++counts[input[i]]; + + for (int i = 0; i < day; ++i) { + std::rotate(counts, counts + 1, counts + 9); + counts[6] += counts[8]; + } + + return std::accumulate(counts, counts + 9, 0ull); +} + +int main() +{ + std::cout << "80: " << countFish(80) << std::endl; + std::cout << "256: " << countFish(256) << std::endl; + return 0; +} + diff --git a/year2021/day6/part1.clj b/year2021/day6/part1.clj new file mode 100644 index 0000000..70293f7 --- /dev/null +++ b/year2021/day6/part1.clj @@ -0,0 +1,20 @@ +(require '[clojure.string :as str]) + +(def fish-init + (->> (read-line) + (#(str/split % #",")) + (map #(Integer/parseInt %)) + (vec) + ) + ) + +(defn cycle-day [fish] + (flatten + (for [f (map dec fish)] + (if (neg? f) [6 8] f) + ) + ) + ) + +(println (count (nth (iterate cycle-day fish-init) 80))) + diff --git a/year2021/day6/part2.clj b/year2021/day6/part2.clj new file mode 100644 index 0000000..ad6fe91 --- /dev/null +++ b/year2021/day6/part2.clj @@ -0,0 +1,25 @@ +(require '[clojure.string :as str]) + +(->> (read-line) + (#(str/split % #",")) + (map read-string) + (reduce #(update %1 %2 inc) (vec (repeat 9 0))) + (iterate + #(let [nf (conj (vec (rest %)) (first %))] + (update nf 6 (partial + (get nf 8))) + ) + ) + (#(nth % 256)) + (apply +) + (println) + ) + +; ->> read input from stdin +; split input string by commas +; convert string array into number array +; reduce to frequency counts +; create iterator that returns next day's counts +; get 256th iteration +; sum all frequency counts +; print results + |