#include #include #include #include #include #include #include #include constexpr int DIVIDEND = 20201227; constexpr int START_SUBJECT_NUMBER = 7; 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; } /** * @param int The puzzle input * @return std::pair Each public key */ std::pair parseInput(const std::vector &input) { assert(input.size() == 2); return std::make_pair(std::stol(input.at(0)), std::stol(input.at(1))); } /** * Perform the transform * @param subjectNumber The subject number * @param numLoops The number of loops * @param initValue The initial value to use * @return long The transform */ long performTransform(int subjectNumber, int numLoops, int initValue = 1) { long res = initValue; for (int i = 0; i < numLoops; i++) { res *= subjectNumber; res %= DIVIDEND; } return res; } /** * Brute force the loop size * @param subjectNumber The subject number * @param target The target number to stop at * @return int THe loop size */ int findLoopSize(int subjectNumber, int target) { long value = 1; for (int loopSize = 1;; loopSize++) { value = performTransform(subjectNumber, 1, value); if (value == target) { return loopSize; } } } long part1(const std::pair &publicKeys) { int cardLoopSize = findLoopSize(START_SUBJECT_NUMBER, publicKeys.first); return performTransform(publicKeys.second, cardLoopSize); } int main(int argc, char *argv[]) { if (argc != 2) { std::cerr << argv[0] << " " << std::endl; return 1; } auto input = readInput(argv[1]); auto parsedInput = parseInput(input); std::cout << part1(parsedInput) << std::endl; }