day1
This commit is contained in:
		
							
								
								
									
										19
									
								
								day1/1.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								day1/1.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										41
									
								
								day1/2.cpp
									
									
									
									
									
										Normal 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;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user