Skip to content

Instantly share code, notes, and snippets.

@markhc
Created December 2, 2019 04:11
Show Gist options
  • Save markhc/e4bd21c976a7f90599eee1db3993cec8 to your computer and use it in GitHub Desktop.
Save markhc/e4bd21c976a7f90599eee1db3993cec8 to your computer and use it in GitHub Desktop.
Solution for day 1 of AoC using C++
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