-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.h
42 lines (38 loc) · 960 Bytes
/
day6.h
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
#include <algorithm>
#include <fstream>
#include <iterator>
#include <string>
#include <unordered_map>
#include <vector>
namespace {
using namespace std;
auto parse(ifstream input) {
unordered_map<string, string> map;
string a;
while (getline(input, a, ')')) {
string b;
getline(input, b);
map.insert({ b, a });
}
return map;
}
int part1(ifstream input) {
auto map = parse(move(input));
int total = 0;
for (auto [a, b] : map) {
total++;
for (; b != "COM"; b = map[b])
total++;
}
return total;
}
int part2(ifstream input) {
auto map = parse(move(input));
vector<string> you, san;
for (auto b = map["YOU"]; b != "COM"; b = map[b])
you.push_back(b);
for (auto b = map["SAN"]; b != "COM"; b = map[b])
san.push_back(b);
return you.size() + san.size() - 2 * distance(rbegin(you), mismatch(rbegin(you), rend(you), rbegin(san)).first);
}
}