Last active
December 1, 2022 22:03
-
-
Save InukVT/7e225a12f64e784df82ca0abe06a584b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <iostream> | |
| #include <vector> | |
| #include <functional> | |
| #include <string> | |
| #include <fstream> | |
| #include <numeric> | |
| #include <algorithm> | |
| class Elfs { | |
| std::vector<uint64_t> elfs; | |
| public: | |
| Elfs() | |
| { | |
| std::fstream file ("list"); | |
| std::vector<int> currentCalory; | |
| if (file.is_open()) | |
| { | |
| while(file) | |
| { | |
| std::string str; | |
| std::getline(file, str); | |
| if(str == "") | |
| { | |
| uint64_t newElf = std::accumulate(currentCalory.begin(), currentCalory.end(), 0) ; | |
| currentCalory.clear(); | |
| elfs.push_back(newElf); | |
| continue; | |
| } | |
| currentCalory.push_back(std::stoi(str)); | |
| } | |
| } | |
| std::sort(elfs.begin(), elfs.end(), std::greater<uint64_t>()); | |
| } | |
| uint64_t biggest() | |
| { | |
| return elfs[0]; | |
| } | |
| uint64_t biggest_three() | |
| { | |
| return std::accumulate(elfs.begin(), elfs.begin() + 3, 0); | |
| } | |
| }; | |
| int main(int argc, const char * argv[]) | |
| { | |
| Elfs elfs; | |
| // insert code here... | |
| uint64_t largest = elfs.biggest(); | |
| uint64_t biggest_three = elfs.biggest_three(); | |
| std::cout << "Biggest calory is: " << largest << std::endl; | |
| std::cout << "Biggest 3 calories is: " << biggest_three << std::endl;; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment