Skip to content

Instantly share code, notes, and snippets.

@ewancook
Last active December 1, 2019 13:28
Show Gist options
  • Save ewancook/e3e2caead581ce36e3fd6e962962432d to your computer and use it in GitHub Desktop.
Save ewancook/e3e2caead581ce36e3fd6e962962432d to your computer and use it in GitHub Desktop.
#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