diff --git a/day1/1.cpp b/day1/1.cpp new file mode 100644 index 0000000..4b7eeae --- /dev/null +++ b/day1/1.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +int main() { + std::ifstream input("./input"); + + size_t val = 0; + for(std::string line; std::getline(input, line);) { + std::vector digits; + std::for_each(line.begin(), line.end(), [&digits](const char c){ + uint8_t digit = c - '0'; + if(digit < 10) digits.emplace_back(digit); + }); + val += digits.front()*10 + digits.back(); + } + std::cout << val << std::endl; +} diff --git a/day1/2.cpp b/day1/2.cpp new file mode 100644 index 0000000..fb36556 --- /dev/null +++ b/day1/2.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +#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 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; +}