# JUNTO Practice: Advent of Code 2019: Day 6 - Universal Orbit Map

Discussed on January 21, 2020.

## Solutions

Click to see:

### John Lekberg

``````const fs = require("fs");

function* parseData(data) {
const lines = data.trim().split("\n");
for (const line of lines) {
yield line.split(")");
}
}

const orbitTree = {};
for (const [center, planet] of parseData(inputData)) {
orbitTree[planet] = center;
}
``````

Part 1:

``````function orbits(planet) {
if (planet in orbitTree) {
return 1 + orbits(orbitTree[planet]);
} else {
return 0;
}
}

let total_orbits = 0;
for (const planet in orbitTree) {
total_orbits += orbits(planet);
}

console.log(total_orbits)
``````

Part 2:

``````function* pathToCOM(planet) {
while (planet !== "COM") {
yield planet;
planet = orbitTree[planet];
}
}

const pathYOU = [...pathToCOM("YOU")];
const pathSAN = [...pathToCOM("SAN")];

function idxNotEqual(arr1, arr2) {
const N = Math.min(arr1.length, arr2.length);
for (let i = 0; i < N; i++) {
if (arr1[i] !== arr2[i]) {
return i;
}
}
}

pathYOU.reverse();
pathSAN.reverse();
const idx = idxNotEqual(pathYOU, pathSAN);

const distance = pathYOU.length + pathSAN.length - 2 * (idx + 1);
console.log(distance);
``````

### Daniel Bassett

``````const input = (() => {})();

const START_ID = 'COM';

const chain = (x, id) => {
const nodes = [];
while(id != START_ID) {
id = x[id].first;
nodes.push(id);
}
return nodes;
};

const parse = (list) => {
x = {};

list.split'\n'.map(letter => {
const sequence = letter.split')';
if(!x[sequence[0]]) {
x[sequence[0]] = {
first: null,
second: []
};
}
if(!x[sequence[1]]) {
x[sequence[1]] = {
first: null,
second: []
};
}
x[sequence[0]].second.push(sequence[1]);
x[sequence[1]].first = sequence[0];
});

return x;
}

const solvePart1 = (hashmap) => {
let sum = 0;
Object.keys(hashmap).map(node => {
sum += chain(hashmap, node).length;
});
return sum;
};

const SANTA = 'SAN';
const YOU = 'YOU':

const solvePart2 = (hashmap) => {
const one = chain(hashmap, SANTA);
const two = chain(hashmap, YOU);

let result;
one.map((node,i) => {
const y = two.findIndex(node2 => node == node2);
if(y != -1 && result == null) {
result = i+y;
}
});
return result;
};

const hashmap = parse(input);
console.log(solvePart1(hashmap));
console.log(solvePart2(hashmap))

//console.log(solvePart2(parse('COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN');
console.log(solvePart2(hashmap));
``````