day 6 and 7
This commit is contained in:
parent
dd9f13cb0d
commit
cbc02f04d5
7 changed files with 2130 additions and 0 deletions
1805
2019/06/input.txt
Normal file
1805
2019/06/input.txt
Normal file
File diff suppressed because it is too large
Load diff
30
2019/06/part1.js
Normal file
30
2019/06/part1.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
const fs = require("fs");
|
||||
const text = fs.readFileSync("2019/06/input.txt", "utf-8");
|
||||
|
||||
let inputArr = text.split('\n');
|
||||
let objNet = {};
|
||||
let objArr = [];
|
||||
inputArr.forEach(input => {
|
||||
points = input.split(')');
|
||||
objNet[points[1]] = points[0];
|
||||
objArr.push(points[1]);
|
||||
})
|
||||
console.log(objNet);
|
||||
total = 0;
|
||||
let i;
|
||||
objArr.forEach(obj => {
|
||||
i=0
|
||||
getOrigin(obj)
|
||||
total+=i
|
||||
});
|
||||
console.log(total)
|
||||
|
||||
function getOrigin(obj) {
|
||||
center = objNet[obj]
|
||||
if (center) {
|
||||
i++
|
||||
return getOrigin(center)
|
||||
} else {
|
||||
return i
|
||||
}
|
||||
}
|
31
2019/06/part2.js
Normal file
31
2019/06/part2.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
const fs = require("fs");
|
||||
const text = fs.readFileSync("2019/06/input.txt", "utf-8");
|
||||
|
||||
let inputArr = text.split('\n');
|
||||
let objNet = {};
|
||||
let objArr = [];
|
||||
inputArr.forEach(input => {
|
||||
points = input.split(')');
|
||||
objNet[points[1]] = {orbits: points[0]};
|
||||
objArr.push(points[1]);
|
||||
})
|
||||
|
||||
function getOrigin(obj, track, transfers = 0) {
|
||||
center = objNet[obj]
|
||||
if (center) {
|
||||
objNet[obj]["distFrom"+track] = transfers-1;
|
||||
transfers++;
|
||||
return getOrigin(center.orbits, track, transfers)
|
||||
} else {
|
||||
return transfers
|
||||
}
|
||||
}
|
||||
getOrigin("SAN", "SAN")
|
||||
getOrigin("YOU", "YOU")
|
||||
|
||||
min = Infinity;
|
||||
objArr.forEach(obj => {
|
||||
totalDist = objNet[obj].distFromSAN + objNet[obj].distFromYOU;
|
||||
min = totalDist < min? totalDist:min;
|
||||
})
|
||||
console.log(min)
|
Loading…
Add table
Add a link
Reference in a new issue