Skip to content

Instantly share code, notes, and snippets.

@willeccles
Created May 4, 2016 21:16
Show Gist options
  • Save willeccles/22241811fbd2de96632086ba49f12ef3 to your computer and use it in GitHub Desktop.
Save willeccles/22241811fbd2de96632086ba49f12ef3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <regex>
#include <string>
#include <fstream>
#include <vector>
#include <ctime>
#include <thread>
using namespace std;
// just to make things nicer for myself later
#define MATCH(str, reg) regex_search(str, reg)
//* PART 1 REGEXES *\\
regex vowels("([^aeiou]*[aeiou]+[^aeiou]*){3,}");
regex pairs(".*([a-zA-Z])\\1.*");
regex illegals(".*(ab|cd|pq|xy)+.*");
//* PART 2 REGEXES *\\
regex repeatedpair(".*([a-zA-Z]{2}).*\\1.*");
regex repwithsinglebetween(".*([a-zA-Z])(?:(?!\\1).)\\1.*");
// number of nice strings
int nice1 = 0; // part 2
int nice2 = 0; // part 1
clock_t start;
vector<string> strings;
void part1();
void part2();
int main() {
cout << "Reading text file... ";
fstream file("Day 5 Input.txt");
string line;
while (getline(file, line))
strings.push_back(line);
cout << "DONE" << endl;
// start processing.
start = clock();
thread p1(part1);
thread p2(part2);
p1.join();
p2.join();
cout << "Total time multithreaded: " << clock() - start << "ms" << endl;
system("pause");
nice1 = 0;
nice2 = 0;
start = clock();
part1();
part2();
cout << "Total time single threaded: " << clock() - start << "ms" << endl;
system("pause"); // because visual studio
return 0;
}
void part1() {
for (auto s : strings) {
if (MATCH(s, vowels) && MATCH(s, pairs) && !MATCH(s, illegals)) {
nice1++;
}
}
cout << "Part 1: " << nice1 << " nice strings. (took " << clock() - start << "ms)" << endl;
}
void part2() {
for (auto s : strings) {
if (MATCH(s, repeatedpair) && MATCH(s, repwithsinglebetween)) {
nice2++;
}
}
cout << "Part 2: " << nice2 << " nice strings. (took " << clock() - start << "ms)" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment