advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

day-01.cc (949B)

    1 #include <fstream>
    2 #include <iostream>
    3 #include <numeric>
    4 #include <vector>
    5 
    6 auto part_1(std::vector<int>& masses) -> int {
    7   return std::accumulate(
    8       masses.begin(), masses.end(), 0,
    9       [](auto acc, auto mass) { return acc + mass / 3 - 2; });
   10 }
   11 
   12 auto part_2(std::vector<int>& masses) -> int {
   13   return std::accumulate(masses.begin(), masses.end(), 0,
   14                          [](auto acc, auto mass) {
   15                            while (mass > 0) {
   16                              mass = mass / 3 - 2;
   17                              acc += mass;
   18                            }
   19                            return acc;
   20                          });
   21 }
   22 
   23 auto main() -> int {
   24   auto input_file = std::ifstream("input.txt");
   25   auto input = std::vector<int>{};
   26   int x;
   27   while (input_file >> x) {
   28     input.push_back(x);
   29   }
   30 
   31   std::cout << "C++17:\n"
   32             << "Part 1: " << part_1(input) << "\nPart 2: " << part_2(input)
   33             << std::endl;
   34 }