Created
December 2, 2019 04:11
-
-
Save markhc/e4bd21c976a7f90599eee1db3993cec8 to your computer and use it in GitHub Desktop.
Solution for day 1 of AoC using C++
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
std::string run(std::string input) override | |
{ | |
using namespace ranges; | |
auto const not_empty = [](auto&& l) { return !l.empty(); }; | |
auto const to_integer = [](auto&& l) { return std::stoi(l); }; | |
auto const calc_fuel = [](auto&& mass) { | |
auto fuel = mass / 3 - 2; | |
auto fuelMass = fuel; | |
while (fuelMass / 3 - 2 > 0) { | |
fuel += fuelMass / 3 - 2; | |
fuelMass = fuelMass / 3 - 2; | |
} | |
return fuel; | |
}; | |
auto lines = extract_all_lines(input); | |
auto masses = lines | views::filter(not_empty) | views::transform(to_integer); | |
auto fuel = masses | views::transform(calc_fuel); | |
auto total = accumulate(fuel, 0); | |
return std::to_string(total); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment