diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-17 22:05:44 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-17 22:05:44 +0800 |
commit | cdafa16d744a0ad8b240d9a6f63097147e91dfba (patch) | |
tree | 17bc4028c70afd97d3bdddcc5849831d21019aae /src | |
parent | 814ce63def18aef5d28f83eb42c7c35c126a63ae (diff) | |
download | advent-of-code-cdafa16d744a0ad8b240d9a6f63097147e91dfba.tar.gz advent-of-code-cdafa16d744a0ad8b240d9a6f63097147e91dfba.zip |
2021 day6 part1
Diffstat (limited to 'src')
-rw-r--r-- | src/2021/day6/README.md | 6 | ||||
-rw-r--r-- | src/2021/day6/aoc.cpp | 47 | ||||
-rw-r--r-- | src/2021/day6/aoc.h | 2 |
3 files changed, 55 insertions, 0 deletions
diff --git a/src/2021/day6/README.md b/src/2021/day6/README.md index 019eee4..f61cc2c 100644 --- a/src/2021/day6/README.md +++ b/src/2021/day6/README.md @@ -48,4 +48,10 @@ In this example, after 18 days, there are a total of 26 fish. After 80 days, the Find a way to simulate lanternfish. How many lanternfish would there be after 80 days? +--- Part Two --- +Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean? + +After 256 days in the example above, there would be a total of 26984457539 lanternfish! + +How many lanternfish would there be after 256 days? diff --git a/src/2021/day6/aoc.cpp b/src/2021/day6/aoc.cpp index 7a3c895..98ece02 100644 --- a/src/2021/day6/aoc.cpp +++ b/src/2021/day6/aoc.cpp @@ -1,5 +1,52 @@ #include "aoc.h" +#include <algorithm> +#include <vector> namespace aoc2021 { +static void run(int day, std::vector<int>& v) { + // std::for_each(v.begin(), v.end(), [](int i) { printf("%d ", i); }); + // printf(": %zu\n", v.size()); + if (day > 0) { + int count{0}; + for (auto& i : v) { + if (i > 0) { + i--; + } else { + i = 6; + count += 1; + } + } + while (count > 0) { + v.push_back(8); + count--; + } + run(day - 1, v); + } } + +size_t day6(int i, int days) { + std::vector<int> v = {i}; + run(days, v); + return v.size(); +} + +size_t day6(line_view file, int days) { + size_t ns[7] = {0}; + for (int i : {0, 1, 2, 3, 4, 5, 6}) { + ns[i] = day6(i, days); + printf("--> %d : %zu\n", i, ns[i]); + } + + size_t total{0}; + const char* p = file.line; + while (p < file.line + file.length) { + if (*p >= '0' && *p <= '9') { + total += ns[*p - '0']; + } + p++; + } + return total; +} + +} // namespace aoc2021 diff --git a/src/2021/day6/aoc.h b/src/2021/day6/aoc.h index cd205cd..6c02437 100644 --- a/src/2021/day6/aoc.h +++ b/src/2021/day6/aoc.h @@ -3,4 +3,6 @@ namespace aoc2021 { +size_t day6(int, int); +size_t day6(line_view, int); } |