CPP Version of day 1 done.
This commit is contained in:
parent
3ce89fa056
commit
bd60b0dc5e
1 changed files with 83 additions and 1 deletions
|
@ -1,7 +1,89 @@
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
struct Elf {
|
||||||
|
public:
|
||||||
|
Elf()
|
||||||
|
: pocket({})
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void add(uint64_t item)
|
||||||
|
{
|
||||||
|
pocket.push_back(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t getTotal() const
|
||||||
|
{
|
||||||
|
uint64_t total(0);
|
||||||
|
for (const auto& item : pocket)
|
||||||
|
total += item;
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
pocket.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<uint64_t> pocket;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::ifstream open_file();
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << "Bonjour Monde!" << std::endl;
|
std::ifstream file;
|
||||||
|
std::string file_content("");
|
||||||
|
std::vector<uint64_t> elvesTotal;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
file = open_file();
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::cerr << "Could not open file." << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gathering each elf into the elves vector
|
||||||
|
Elf tempelf;
|
||||||
|
while (std::getline(file, file_content))
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
tempelf.add(std::stoull(file_content));
|
||||||
|
}
|
||||||
|
catch(std::exception ex)
|
||||||
|
{
|
||||||
|
elvesTotal.push_back(tempelf.getTotal());
|
||||||
|
tempelf.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(elvesTotal.begin(), elvesTotal.end() , [](uint64_t a, uint64_t b) { return a>b; });
|
||||||
|
std::cout << "Elf Max: " << *elvesTotal.begin() << std::endl;
|
||||||
|
uint64_t maxThree(0);
|
||||||
|
for(auto it = elvesTotal.begin() ; it != elvesTotal.end() ; it++)
|
||||||
|
{
|
||||||
|
maxThree += *it;
|
||||||
|
if(it == elvesTotal.begin()+2)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "Three elves total: " << maxThree << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ifstream open_file()
|
||||||
|
{
|
||||||
|
std::ifstream file;
|
||||||
|
file.open("elves");
|
||||||
|
if (!file.is_open())
|
||||||
|
throw -1;
|
||||||
|
return file;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue