adventofcode-2017/day20/part1.js

16 lines
616 B
JavaScript
Raw Permalink Normal View History

2017-12-25 23:22:40 +00:00
var input = require("fs").readFileSync("input.txt").toString();
var inputjson = "[{" + input.replace(/([a-z])=</g, "\"$1\": [").replace(/>/g, "]").replace(/\n/g, "},\n{").slice(0,-3) + "]";
var objects = JSON.parse(inputjson);
for(var i = 0; i < 1000; i++){
objects = objects.map(o=>{
o.v = o.v.map((v,x)=>(v + o.a[x]));
o.p = o.p.map((p,x)=>(p + o.v[x]));
return o;
});
}
var locations = objects.map(o=>(o.p));
var distances = locations.map((a)=>(a.map((b)=>(Math.abs(b))).reduce((b,c)=>(b+c))));
var maxdist = Math.min.apply(null, distances);
console.log(maxdist);
console.log(distances.indexOf(maxdist));