Created
December 14, 2022 17:49
-
-
Save mnd999/9b865e1c93561dd97264ec00590a1d47 to your computer and use it in GitHub Desktop.
Advent of code 2022 Day 1
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 <fstream> | |
#include <iostream> | |
#include <list> | |
#include <sstream> | |
#include <string> | |
int main(int argc, char *argv[]) { | |
std::list<long> totals; | |
std::string line; | |
long tot = 0; | |
while (std::getline(std::cin, line)) { | |
if (line.length() > 0) { | |
std::istringstream iss(line); | |
int a; | |
if (!(iss >> a)) { | |
break; | |
} // error | |
tot += a; | |
} else { | |
totals.push_back(tot); | |
tot = 0; | |
} | |
// std::cout << tot << "\n"; | |
} | |
totals.sort(); | |
totals.reverse(); | |
long grandtot; | |
int i = 0; | |
for (std::list<long>::iterator it = totals.begin(); i < 3; ++it) { | |
grandtot += *it; | |
i++; | |
} | |
std::cout << grandtot; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment