repl.it
@bearbearmo/

Advent of code 2019: day 6

Nodejs

No description

fork
loading
Files
  • index.js
index.js
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
const input = `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`.split("\n").map(x => x.split(")")); // 54 orbits, 4 orbits between YOU and SAN

function getOrbits(i, mode){
	let route = [input[i][0]], total = 1;
	for(; input[i][0] !== 'COM'; route.unshift(input[i][0]), total++){
		for(let j = 0; j < input.length; j++){
			if(input[i][0] === input[j][1]) {
				i = j;
				break;
			}
		}
	}
	return mode ? route : total;
}

let you, san, orbits = 0;

for(let i = 0; i < input.length; i++){
	if(input[i][1] === "YOU") you = getOrbits(i, 1);
	else if(input[i][1] === "SAN") san = getOrbits(i, 1);
	orbits += getOrbits(i, 0);
}

console.log(orbits + " orbits")

for(let i = 0; i < you.length; i++){
	if(san[i] !== you[i]) {
		console.log((you.length - i + san.length - i) + " orbits between you and santa");
		break
	}
}
?