main.cpp (1629B)
1 #include <iostream> 2 #include <filesystem> 3 #include <fstream> 4 #include <vector> 5 #include <cstring> 6 #include <cstdio> 7 #include <tuple> 8 #include <algorithm> 9 #include <regex> 10 11 typedef std::vector<std::string> Data; 12 13 template<typename T> 14 T read_file(const std::string &path) { 15 std::ifstream ifs(path, std::ios::binary); 16 if (!ifs.is_open()) { 17 throw std::runtime_error(path+":"+std::strerror(errno)); 18 } 19 T buf; 20 while (1) { 21 std::string str; 22 std::getline(ifs, str); 23 if (!str.empty()) buf.push_back(str); 24 if (!ifs) break; 25 } 26 return buf; 27 } 28 29 int part1(Data &input [[ maybe_unused ]]) { 30 int r=0; 31 const std::regex re1("([aeiou]).*?([aeiou]).*?([aeiou])"); 32 const std::regex re2("([a-z])\\1"); 33 const std::regex re3("^(?!.*(ab|cd|pq|xy)).*$"); 34 for (auto &s:input) { 35 std::smatch m1; 36 if (std::regex_search(s, m1, re1) && m1.size()>3 37 && std::regex_search(s, re2) 38 && std::regex_search(s, re3) 39 ) ++r; 40 } 41 return r; 42 } 43 44 int part2(Data &input [[ maybe_unused ]]) { 45 int r=0; 46 const std::regex re1("([a-z]{2}).*?\\1"); 47 const std::regex re2("([a-z]).\\1"); 48 for (auto &s:input) { 49 if (std::regex_search(s, re1) && std::regex_search(s, re2)) ++r; 50 } 51 return r; 52 } 53 54 int main(int argc, char **argv) { 55 const std::string fname = argc>1 ? argv[1] : "test1.txt"; 56 std::cout << "AoC 2015 day 05 " << fname << std::endl; 57 Data input = read_file<Data>(fname); 58 std::cout << "part1: " << part1(input) << std::endl; 59 std::cout << "part2: " << part2(input) << std::endl; 60 61 return 0; 62 }