This commit is contained in:
Finn Dane 2023-12-03 20:56:13 +01:00
parent 78b3f629e0
commit 2b69263c12
Signed by: Finn_Dane
SSH Key Fingerprint: SHA256:oStA/u8gviEgqATh8eBxIRhhzQVWuUQeL1zVlP7kYtw
2 changed files with 60 additions and 0 deletions

19
day1/1.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
int main() {
std::ifstream input("./input");
size_t val = 0;
for(std::string line; std::getline(input, line);) {
std::vector<uint8_t> 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;
}

41
day1/2.cpp Normal file
View File

@ -0,0 +1,41 @@
#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;
}