Created
December 1, 2022 10:26
-
-
Save HappyCerberus/2ec5d34823f191e6ba1fc8adadc90a97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uint64_t max_calories(const std::vector<std::string>& data) { | |
auto by_elf = data | | |
// group by elf: range{range{string}} | |
std::views::lazy_split(std::string{}) | | |
// sum up the calories for each elf: range{uint64_t} | |
std::views::transform([](const auto& elf) -> uint64_t { | |
// std::string -> uint64_t | |
auto to_unsigned = | |
[](const auto& in) { return std::stoull(in); }; | |
// make a view of uint64_t: range{string} -> range{uint64_t} | |
auto rng = elf | | |
std::views::transform(to_unsigned) | | |
std::views::common; // std::reduce requires a common range | |
// reduce into the sum: range{uint64_t} -> uint64_t | |
return std::reduce(rng.begin(), rng.end()); | |
}); | |
// find the elf with the maximum number of calories | |
auto it = std::ranges::max_element(by_elf); | |
return *it; // and return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment