advent-of-code

Perserverance, or the lack thereof

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

day-02.cc (1180B)

    1 #include <fstream>
    2 #include <iostream>
    3 #include <vector>
    4 
    5 auto computer(std::vector<int> input, int noun, int verb) {
    6   input[1] = noun;
    7   input[2] = verb;
    8 
    9   auto pc = 0;
   10   while (true) {
   11     if (input[pc] == 1) {
   12       input[input[pc + 3]] = input[input[pc + 1]] + input[input[pc + 2]];
   13       pc += 4;
   14     } else if (input[pc] == 2) {
   15       input[input[pc + 3]] = input[input[pc + 1]] * input[input[pc + 2]];
   16       pc += 4;
   17     } else if (input[pc] == 99) {
   18       break;
   19     }
   20   }
   21 
   22   return input[0];
   23 }
   24 
   25 auto part_1(std::vector<int>& input) -> int {
   26   return computer(input, 12, 2);
   27 }
   28 
   29 auto part_2(std::vector<int>& input) -> int {
   30   for (auto noun = 0; noun <= 99; ++noun) {
   31     for (auto verb = 0; verb <= 99; ++verb) {
   32       if (computer(input, noun, verb) == 19690720) {
   33         return noun * 100 + verb;
   34       }
   35     }
   36   }
   37   return 0;
   38 }
   39 
   40 auto main() -> int {
   41   auto input_file = std::ifstream("input.txt");
   42   auto input = std::vector<int>{};
   43   for (std::string op; std::getline(input_file, op, ',');) {
   44     input.push_back(std::stoi(op));
   45   }
   46 
   47   std::cout << "C++17:\n"
   48             << "Part 1: " << part_1(input) << "\nPart 2: " << part_2(input)
   49             << std::endl;
   50 }