Skip to content

Instantly share code, notes, and snippets.

@InukVT
Last active December 1, 2022 22:03
Show Gist options
  • Select an option

  • Save InukVT/7e225a12f64e784df82ca0abe06a584b to your computer and use it in GitHub Desktop.

Select an option

Save InukVT/7e225a12f64e784df82ca0abe06a584b to your computer and use it in GitHub Desktop.
#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