-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.cpp
77 lines (68 loc) · 1.61 KB
/
day6.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* day6.cpp
*
* Created on: Dec 6, 2019
* Author: sanja
*/
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
std::unordered_map<std::string, std::vector<std::string>> map;
std::unordered_map<std::string, std::string> map_parents;
std::unordered_set<std::string> visited;
int countOrbits(std::string s, int depth)
{
int sum = 0;
for(std::string str : map[s])
{
sum += countOrbits(str, depth+1) + depth + 1 ;
}
return sum;
}
int countPath(std::string you, std::string san)
{
std::vector<std::string> parents_of_you;
std::vector<std::string> parents_of_san;
while(map_parents[you] != "COM")
{
parents_of_you.push_back(you);
you = map_parents[you];
}
while (map_parents[san] != "COM")
{
parents_of_san.push_back(san);
san = map_parents[san];
}
for(auto i = parents_of_you.begin(); i != parents_of_you.end(); i++)
{
for(auto j = parents_of_san.begin(); j != parents_of_san.end(); j++)
{
if(*i == *j)
{
return std::distance(parents_of_you.begin(), i) + std::distance(parents_of_san.begin(), j) - 2;
}
}
}
}
int main ()
{
std::string line;
std::ifstream file("input.txt");
while (std::getline(file, line)) {
std::string first = line.substr(0, 3);
if(map.find(first) != map.end()) {
map[first].push_back(line.substr(4,3));
} else {
std::vector<std::string> vec;
vec.push_back(line.substr(4,3));
map[first] = vec;
}
std::string sec = line.substr(4,3);
map_parents[sec] = first;
}
std::cout << countOrbits("COM", 0) << std::endl;
std::cout << countPath("YOU", "SAN") << std::endl;
}