diff --git a/day25/Makefile b/day25/Makefile new file mode 100644 index 0000000..11a667e --- /dev/null +++ b/day25/Makefile @@ -0,0 +1,15 @@ +CC=g++ +BIN_NAME=day25 +CCFLAGS=-o $(BIN_NAME) -g -O2 -std=c++17 +LDFLAGS= + +.PHONY: all, clean + +all: $(BIN_NAME) + +clean: + rm -f $(BIN_NAME) + +$(BIN_NAME): day25.cpp + $(CC) $(CCFLAGS) $(LDFLAGS) day25.cpp + diff --git a/day25/day25.cpp b/day25/day25.cpp new file mode 100644 index 0000000..a833b69 --- /dev/null +++ b/day25/day25.cpp @@ -0,0 +1,83 @@ +#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; +}