about summary refs log tree commit diff
path: root/day04.cc
blob: 7d7d48b2ec8c31d0a8913c1bc272aed287971b25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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), {}};
		anagram::set awords{words.cbegin(), words.cend()};
		size_t wcount = distance(istream_iterator<string>(is2), {});

		if (words.size() == wcount) p1++;
		if (awords.size() == wcount) p2++;
	}

	cout << p1 << endl << p2 << endl;
}