Day 2 part 1
This commit is contained in:
40
Day2/Question1/main.cpp
Normal file
40
Day2/Question1/main.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
std::fstream file("input");
|
||||
std::string currentID = "";
|
||||
uint64_t rangeMin = 0, rangeMax = 0, positionOfHypen = 0,
|
||||
sumOfAllInvalidIds = 0, numberOfDigits = 0;
|
||||
double nthSymmetryicalNumber = 0;
|
||||
|
||||
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));
|
||||
|
||||
for (uint64_t number = rangeMin; number <= rangeMax; number++) {
|
||||
numberOfDigits = std::floor(std::log10(number)) + 1;
|
||||
|
||||
// Odd numbers can't satisfy the repeating pattern
|
||||
if (numberOfDigits % 2 == 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Really only possible symmetry is here
|
||||
nthSymmetryicalNumber =
|
||||
number / static_cast<double>(std::pow(10, numberOfDigits >> 1) + 1);
|
||||
|
||||
// Checking if the number is "whole"
|
||||
sumOfAllInvalidIds +=
|
||||
nthSymmetryicalNumber == std::trunc(nthSymmetryicalNumber) ? number
|
||||
: 0;
|
||||
}
|
||||
}
|
||||
std::cout << sumOfAllInvalidIds << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user