]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day6: c++ consteval solution
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 6 Dec 2021 17:41:51 +0000 (12:41 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 6 Dec 2021 17:41:51 +0000 (12:41 -0500)
day6/consteval.cpp [new file with mode: 0644]

diff --git a/day6/consteval.cpp b/day6/consteval.cpp
new file mode 100644 (file)
index 0000000..f46da89
--- /dev/null
@@ -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;
+}
+