Last active
December 1, 2019 13:28
-
-
Save ewancook/e3e2caead581ce36e3fd6e962962432d to your computer and use it in GitHub Desktop.
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 <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <string> | |
#include <cmath> | |
int calculate_fuel(int mass) { | |
return std::floor((float)mass/3-2); | |
} | |
int main() { | |
std::ifstream f("input.txt"); | |
std::vector<int> masses; | |
std::string line; | |
while(std::getline(f, line)) { | |
if (line.size() > 0) { | |
masses.emplace_back(std::stoi(line)); | |
} | |
} | |
int part_one{}, part_two{}, fuel{}; | |
for (auto const& mass: masses) { | |
fuel = calculate_fuel(mass); | |
part_one += fuel; | |
while (fuel > 0) { | |
part_two += fuel; | |
fuel = calculate_fuel(fuel); | |
} | |
} | |
std::cout << "Part 1: " << part_one << '\n'; | |
std::cout << "Part 2: " << part_two << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment