From da62aa3d801b65b0985497cf110bfb2bdaf48ace Mon Sep 17 00:00:00 2001 From: cat Date: Tue, 2 Dec 2025 22:09:19 +0200 Subject: [PATCH] Day 2 part 2 --- Day2/Question2/input | 1 + Day2/Question2/inputTest | 1 + Day2/Question2/main.cpp | 70 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 Day2/Question2/input create mode 100644 Day2/Question2/inputTest create mode 100644 Day2/Question2/main.cpp diff --git a/Day2/Question2/input b/Day2/Question2/input new file mode 100644 index 0000000..9352771 --- /dev/null +++ b/Day2/Question2/input @@ -0,0 +1 @@ +16100064-16192119,2117697596-2117933551,1-21,9999936269-10000072423,1770-2452,389429-427594,46633-66991,877764826-877930156,880869-991984,18943-26512,7216-9427,825-1162,581490-647864,2736-3909,39327886-39455605,430759-454012,1178-1741,219779-244138,77641-97923,1975994465-1976192503,3486612-3602532,277-378,418-690,74704280-74781349,3915-5717,665312-740273,69386294-69487574,2176846-2268755,26-45,372340114-372408052,7996502103-7996658803,7762107-7787125,48-64,4432420-4462711,130854-178173,87-115,244511-360206,69-86 diff --git a/Day2/Question2/inputTest b/Day2/Question2/inputTest new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/Day2/Question2/inputTest @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 diff --git a/Day2/Question2/main.cpp b/Day2/Question2/main.cpp new file mode 100644 index 0000000..d78d033 --- /dev/null +++ b/Day2/Question2/main.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +std::vector findFactors(uint64_t number) { + // Technically acending order would be better than decending order for + // algorithm below + std::vector factorList; + for (uint64_t possibleFactor = std::ceil(number / 2.0f); possibleFactor > 1; + possibleFactor--) { + if (number % possibleFactor == 0) { + factorList.push_back(possibleFactor); + } + } + + // Certain that every whole number is divisble by 1 lol + factorList.push_back(1); + return factorList; +} + +int main() { + std::fstream file("input"); + std::string currentID = "", currentNumber = "", chunk = ""; + uint64_t rangeMin = 0, rangeMax = 0, positionOfHypen = 0, numberOfDigits = 0, + sumOfAllInvalidIds = 0; + bool isInvalid = false; + + while (std::getline(file, currentID, ',')) { + // I'm sure there is a better way to do this + positionOfHypen = currentID.find('-'); + rangeMin = std::stoull(currentID.substr(0, positionOfHypen)); + rangeMax = std::stoull(currentID.substr(positionOfHypen + 1)); + + // I'm not fan of the loops I've done here + for (uint64_t number = rangeMin; number <= rangeMax; number++) { + numberOfDigits = std::floor(std::log10(number)) + 1; + + // Each factor of the total digits needs to be checked + for (uint64_t factor : findFactors(numberOfDigits)) { + currentNumber = std::to_string(number); + chunk = currentNumber.substr(0, factor); + isInvalid = false; + + // Comparing chuncks to eachother + for (uint64_t index = factor; index < numberOfDigits; index += factor) { + isInvalid = chunk == currentNumber.substr(index, factor); + + // Break if it is valid with these chunks + if (!isInvalid) { + break; + } + } + + // Break if we proved thats invalid above + if (isInvalid) { + break; + } + } + + sumOfAllInvalidIds += isInvalid ? number : 0; + } + } + std::cout << sumOfAllInvalidIds << std::endl; + return 0; +}