#include #include #include #include #include #include #include #include #include #include constexpr char IGNORE_CHAR = 'X'; constexpr auto MASK_PATTERN = R"(mask = ([X0-9]+))"; constexpr auto MEM_PATTERN = R"(mem\[(\d+)\] = (\d+))"; std::vector readInput(const std::string &filename) { std::vector input; std::string line; std::ifstream file(filename); while (std::getline(file, line)) { input.push_back(line); } return input; } std::vector parseStartingNumbers(const std::vector &input) { std::vector rawNumbers; folly::split(",", input.at(0), rawNumbers); std::vector startingNumbers; std::transform( rawNumbers.cbegin(), rawNumbers.cend(), std::back_inserter(startingNumbers), [](const std::string &rawNumber) { return std::stoi(rawNumber); }); return startingNumbers; } int part1(const std::vector &startingNumbers) { std::unordered_map turnSpoken; int lastNumber = startingNumbers.back(); for (std::vector::size_type turn = 1; turn < startingNumbers.size(); turn++) { turnSpoken.emplace(startingNumbers.at(turn - 1), turn); } for (int turn = startingNumbers.size() + 1; turn <= 2020; turn++) { int number = 0; if (turnSpoken.find(lastNumber) != turnSpoken.end()) { number = (turn - 1) - turnSpoken.at(lastNumber); } turnSpoken[lastNumber] = turn - 1; lastNumber = number; } return lastNumber; } int main(int argc, char *argv[]) { if (argc != 2) { std::cerr << argv[0] << " " << std::endl; return 1; } auto input = readInput(argv[1]); auto startingNumbers = parseStartingNumbers(input); std::cout << part1(startingNumbers) << std::endl; }