Files

69 lines
2.0 KiB
C++

#include <cmath>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
std::vector<uint64_t> findFactors(uint64_t number) {
// Technically acending order would be better than decending order for
// algorithm below
std::vector<uint64_t> 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;
}