To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 145df2b2 authored by amiessen's avatar amiessen
Browse files

added day 6, part 2

parent 169b4f02
...@@ -41,7 +41,7 @@ int loadData(const std::string & data_path, std::vector<std::string> & data) { ...@@ -41,7 +41,7 @@ int loadData(const std::string & data_path, std::vector<std::string> & data) {
void findAllOrbits(const std::vector<std::string> & objects, const int dim, void findAllOrbits(const std::vector<std::string> & objects, const int dim,
std::multimap<int, std::string> & orbitMap, std::multimap<std::string, int> & orbitMap,
int orbCount, std::string & innerObj) int orbCount, std::string & innerObj)
{ {
// Get number of direct and indirect orbits for every object. // Get number of direct and indirect orbits for every object.
...@@ -52,11 +52,11 @@ void findAllOrbits(const std::vector<std::string> & objects, const int dim, ...@@ -52,11 +52,11 @@ void findAllOrbits(const std::vector<std::string> & objects, const int dim,
// //
// The pairs (number of (in)direct orbits, object) gets stored in the // The pairs (number of (in)direct orbits, object) gets stored in the
// std::multimap orbitMap // std::multimap orbitMap
int orbitCount = orbCount; int orbitCount = orbCount;
std::vector<int> indices; std::vector<int> indices;
orbitMap.insert(std::pair<int, std::string>(orbitCount, innerObj)); orbitMap.insert(std::pair<std::string, int>(innerObj, orbitCount));
for (int i = 0; i < dim; i++) { for (int i = 0; i < dim; i++) {
if (objects[i] == innerObj) { indices.push_back(i+dim); } if (objects[i] == innerObj) { indices.push_back(i+dim); }
...@@ -65,25 +65,78 @@ void findAllOrbits(const std::vector<std::string> & objects, const int dim, ...@@ -65,25 +65,78 @@ void findAllOrbits(const std::vector<std::string> & objects, const int dim,
orbitCount++; orbitCount++;
for (size_t i = 0; i < indices.size(); i++) { for (size_t i = 0; i < indices.size(); i++) {
innerObj = objects[indices[i]]; innerObj = objects[indices[i]];
findAllOrbits(objects, dim, orbitMap, orbitCount, innerObj); findAllOrbits(objects, dim, orbitMap, orbitCount, innerObj);
} }
} }
int getChecksum(const std::multimap<int, std::string> & orbitMap) { int getChecksum(const std::multimap<std::string, int> & orbitMap) {
int sum = 0; int sum = 0;
// sum of all indirect orbits // sum of all indirect orbits
std::multimap<int, std::string>::const_iterator it = orbitMap.begin(); std::multimap<std::string, int>::const_iterator it = orbitMap.begin();
for(; it != orbitMap.end(); ++it) { for(; it != orbitMap.end(); ++it) {
sum += it->first; sum += it->second;
} }
return sum; return sum;
} }
int getOrbitalDist(const std::vector<std::string> & objects, const int dim,
const std::multimap<std::string, int> & orbitMap,
const std::string & o1, const std::string & o2)
{
// Get distance between objects that o1 and o2 are orbiting. Hence,
// o1 and o2 are found in the second half of std::vector objects.
// NOTE: o1 and o2 may have different distances (numbers of indirect orbits)
// between them and the branching at which their two branches join.
std::vector<int> index(2);
int diff = 0;
std::string obj1, obj2;
auto orbit1 = orbitMap.find(o1);
auto orbit2 = orbitMap.find(o2);
// regardless of number of indirect orbits of o1 and o2, respectively,
// redefine obj1 to always be the furthest away from COM
if (orbit1->second < orbit2->second) {
obj1 = o2;
obj2 = o1;
diff = (orbit2->second) - (orbit1->second);
} else {
obj1 = o1;
obj2 = o2;
diff = (orbit1->second) - (orbit2->second);
}
for (int i = 0; i < diff; i++) {
for (int i = dim; i < 2*dim; i++) {
if (objects[i] == obj1) { index[0] = i-dim; }
}
obj1 = objects[index[0]];
} // FOR: i < diff
int count = diff;
while (obj1 != obj2) {
count += 2;
for (int i = dim; i < 2*dim; i++) {
if (objects[i] == obj1) { index[0] = i-dim; }
else if (objects[i] == obj2) { index[1] = i-dim; }
}
obj1 = objects[index[0]];
obj2 = objects[index[1]];
}
count -= 2; // subtract first two steps from o1 (YOU) and o2 (SAN) inward
return count;
}
// ################ // ################
// ----- MAIN ----- // ----- MAIN -----
...@@ -95,13 +148,15 @@ int main() { ...@@ -95,13 +148,15 @@ int main() {
std::vector<std::string> objects; std::vector<std::string> objects;
int dim = loadData(data_path, objects); int dim = loadData(data_path, objects);
std::multimap<int, std::string> orbitMap; std::multimap<std::string, int> orbitMap;
int orbitCount = 0; int orbitCount = 0;
std::string innermostObj = "COM"; std::string innermostObj = "COM";
findAllOrbits(objects, dim, orbitMap, orbitCount, innermostObj); findAllOrbits(objects, dim, orbitMap, orbitCount, innermostObj);
int checksum = getChecksum(orbitMap); int checksum = getChecksum(orbitMap);
int distSanta = getOrbitalDist(objects, dim, orbitMap, "YOU", "SAN");
std::cout << "Sum of all direct and indirect orbits is " << checksum << "\n"; std::cout << "Sum of all direct and indirect orbits: " << checksum << "\n";
std::cout << "Orbital distance to Santa: " << distSanta << "\n";
} }
...@@ -9,3 +9,5 @@ D)I ...@@ -9,3 +9,5 @@ D)I
E)J E)J
J)K J)K
K)L K)L
K)YOU
I)SAN
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment