about summary refs log tree commit diff
path: root/day04.cc
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-12-05 12:14:46 +0100
committerLeah Neukirchen <leah@vuxu.org>2017-12-05 12:14:46 +0100
commit73e28533374104587c3baf42b29ff6094f4d54b9 (patch)
tree39050722a62432edcfba9b446992c0a3ffb120c1 /day04.cc
parentfef0d1516af784639da5cc56611da625073a3b3c (diff)
downloadadventofcode2017-73e28533374104587c3baf42b29ff6094f4d54b9.tar.gz
adventofcode2017-73e28533374104587c3baf42b29ff6094f4d54b9.tar.xz
adventofcode2017-73e28533374104587c3baf42b29ff6094f4d54b9.zip
day04
Diffstat (limited to 'day04.cc')
-rw-r--r--day04.cc48
1 files changed, 48 insertions, 0 deletions
diff --git a/day04.cc b/day04.cc
new file mode 100644
index 0000000..85ae224
--- /dev/null
+++ b/day04.cc
@@ -0,0 +1,48 @@
+#include <algorithm>
+#include <iostream>
+#include <iterator>
+#include <numeric>
+#include <set>
+#include <sstream>
+#include <string>
+#include <unordered_set>
+#include <vector>
+
+using namespace std;
+
+namespace anagram {
+	struct is_anagram {
+		bool operator()(const string &a, const string &b) const {
+			return is_permutation(begin(a), end(a), begin(b));
+		}
+	};
+
+	struct unordered_hash {
+		size_t operator()(const string &a) const {
+			return accumulate(begin(a), end(a), 0);
+		}
+	};
+
+	using set = unordered_set<string,unordered_hash,is_anagram>;
+}
+
+int
+main()
+{
+	string line;
+	int p1 = 0, p2 = 0;
+
+	while (getline(cin, line)) {
+		istringstream is1{line}, is2{line};
+		set<string> words{istream_iterator<string>(is1),
+		    istream_iterator<string>()};
+		anagram::set awords{words.cbegin(), words.cend()};
+		size_t wcount = distance(istream_iterator<string>(is2),
+		    istream_iterator<std::string>());
+
+		if (words.size() == wcount) p1++;
+		if (awords.size() == wcount) p2++;
+	}
+
+	cout << p1 << endl << p2 << endl;
+}