adventofcode2023/day1/2.cpp

42 lines
1.0 KiB
C++
Raw Normal View History

2023-12-03 20:56:13 +01:00
#include <iostream>
#include <fstream>
#include <regex>
#include <algorithm>
#define STRINGTONUM(test, number) \
if(str == test) { \
digit = number; \
} else \
int main() {
std::ifstream input("./input");
std::regex numberRegex("(zero|one|two|three|four|five|six|seven|eight|nine|[0-9])");
size_t val = 0;
for(std::string line; std::getline(input, line);) {
std::vector<uint8_t> digits;
std::for_each(std::sregex_iterator(line.begin(), line.end(), numberRegex), std::sregex_iterator(), [&digits](const std::smatch &match){
std::string str = match.str();
uint8_t digit;
if(str.size() > 1) { // not a direct digit
STRINGTONUM("zero", 0)
STRINGTONUM("one", 1)
STRINGTONUM("two", 2)
STRINGTONUM("three", 3)
STRINGTONUM("four", 4)
STRINGTONUM("five", 5)
STRINGTONUM("six", 6)
STRINGTONUM("seven", 7)
STRINGTONUM("eight", 8)
STRINGTONUM("nine", 9) {}
} else {
digit = str[0] - '0';
}
digits.push_back(digit);
});
val += digits.front()*10 + digits.back();
}
std::cout << val << std::endl;
}