#include #include #include #include #include #include int main() { const std::string filename = "data.txt"; std::ifstream ifs(filename); if(!ifs.is_open()) { std::cerr << "Missing " << filename << "." << std::endl; return -1; } unsigned long total = 0; unsigned long total_pt2 = 0; long dos = 0; long donts = 0; for(std::string line; std::getline(ifs, line); ) { if(line == "") continue; std::regex pattern("do\\(\\)|don\\'t\\(\\)|mul\\((\\d+),(\\d+)\\)"); auto pattern_begin = std::sregex_iterator(line.begin(), line.end(), pattern); auto pattern_end = std::sregex_iterator(); bool multiplying = true; for (std::sregex_iterator i = pattern_begin; i != pattern_end; ++i) { std::string result = i->str(); std::cout << result << std::endl; if(i->str() == "do()") { ++dos; std::cout << "\033[0m"; multiplying = true; } else if(i->str() == "don't()") { ++donts; std::cout << "\033[1;31m"; multiplying = false; } else { std::smatch::iterator ip_it = i->begin(); std::vector numbers; for (std::advance(ip_it, 1); ip_it != i->end(); advance(ip_it, 1)) { std::string n = *ip_it; numbers.push_back(std::atoi(n.c_str())); } long result = 1; for(auto n : numbers) result *= n; std::cout << result << std::endl; if(multiplying) total_pt2 += result; total += result; } } } std::cout << " Total: " << total << std::endl; std::cout << "PT2 Total: " << total_pt2 << std::endl; std::cout << "do()s: " << dos << std::endl; std::cout << "don't()s: " << donts << std::endl; return 0; }