Day 3 Question 2
This commit is contained in:
48
Day3/Question2/main.cpp
Normal file
48
Day3/Question2/main.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// Cool thing is that you can tweak this number to find any possible digit
|
||||
// version of this question (so yes you can solve part 1 with this)
|
||||
#define JOLTAGE_MUST_LENGHT 12
|
||||
|
||||
int main() {
|
||||
std::fstream input("input");
|
||||
std::string currentLine = "", subSection = "";
|
||||
uint64_t totalJoltage = 0, lineLength = 0;
|
||||
while (std::getline(input, currentLine)) {
|
||||
std::string joltage = "";
|
||||
lineLength = currentLine.length();
|
||||
|
||||
// We need to iteratively find where each digit can be at minimum and max
|
||||
uint16_t availableMax = lineLength - JOLTAGE_MUST_LENGHT, digitPointer = 0;
|
||||
for (uint16_t currentDigit = 1; currentDigit <= JOLTAGE_MUST_LENGHT;
|
||||
currentDigit++) {
|
||||
// Adjusting how much of the digits are left unparsed
|
||||
availableMax =
|
||||
lineLength - (JOLTAGE_MUST_LENGHT - currentDigit + digitPointer);
|
||||
|
||||
// All possible digit candidates
|
||||
subSection = currentLine.substr(digitPointer, availableMax);
|
||||
char biggestDigit = '0';
|
||||
uint16_t positionOfTheBiggestDigit = digitPointer;
|
||||
|
||||
// Find the biggest MOST SIGNIFICANT digit
|
||||
for (char digit : subSection) {
|
||||
if (biggestDigit < digit) {
|
||||
biggestDigit = digit;
|
||||
positionOfTheBiggestDigit = digitPointer;
|
||||
}
|
||||
digitPointer++;
|
||||
}
|
||||
|
||||
// Return to where we got the digit
|
||||
digitPointer = positionOfTheBiggestDigit + 1;
|
||||
joltage.append(1, biggestDigit);
|
||||
}
|
||||
totalJoltage += std::stoull(joltage);
|
||||
}
|
||||
std::cout << totalJoltage << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user