This commit is contained in:
Tim Stallard 2018-12-08 20:34:24 +00:00
parent edd783b3e4
commit 182de3db5f
8 changed files with 186 additions and 0 deletions

6
06/input-test.txt Normal file
View File

@ -0,0 +1,6 @@
1, 1
1, 6
8, 3
3, 4
5, 5
8, 9

50
06/input.txt Normal file
View File

@ -0,0 +1,50 @@
353, 177
233, 332
178, 231
351, 221
309, 151
105, 289
91, 236
321, 206
156, 146
94, 82
81, 114
182, 122
81, 153
319, 312
334, 212
275, 93
224, 355
347, 94
209, 65
118, 172
113, 122
182, 320
191, 178
99, 70
260, 184
266, 119
177, 178
313, 209
61, 285
155, 218
354, 198
274, 53
225, 138
228, 342
187, 165
226, 262
143, 150
124, 159
325, 210
163, 176
326, 91
170, 193
84, 265
199, 248
107, 356
45, 340
277, 173
286, 44
242, 150
120, 230

45
06/part1.js Normal file
View File

@ -0,0 +1,45 @@
var input = require("fs").readFileSync("input.txt").toString();
var points = input.split("\n").filter(a=>a).map(a=>(a.split(",").map(b=>(parseInt(b)))));
var xmin = points.map(a=>(a[0])).sort((a,b)=>(a-b))[0];
var xmax = points.map(a=>(a[0])).sort((a,b)=>(b-a))[0];
var ymin = points.map(a=>(a[1])).sort((a,b)=>(a-b))[0];
var ymax = points.map(a=>(a[1])).sort((a,b)=>(b-a))[0];
var finitePoints = points.filter(p=>(p[0] != xmin && p[0] != xmax && p[1] != ymin && p[1] != ymax));
var infinitePoints = [];
var areas = {};
var man = (xa, ya, xb, yb)=>(Math.abs(xa - xb) + Math.abs(ya - yb));
for(var x = xmin - 200; x <= xmax + 200; x++){
for(var y = ymin - 200; y <= ymax + 200; y++){
var mindist = points.map(p=>(man(p[0], p[1], x, y))).sort((a,b)=>(a-b))[0];
var closest = points.filter(p=>(man(p[0], p[1], x, y) == mindist));
//if(closest.length == 1 && (finitePoints.indexOf(closest[0]) != -1)){
if(closest.length == 1){
if(x == xmin || x == xmax || y == ymin || y == ymax){
if(infinitePoints.indexOf(closest[0]) == -1){
infinitePoints.push(closest[0]);
}
}
if(!areas[closest[0]]){
areas[closest[0]] = 0;
}
areas[closest[0]]++;
}
}
}
for(var point of infinitePoints){
delete areas[point];
}
console.log(areas);
console.log(infinitePoints);
console.log(xmin, xmax, ymin, ymax);
console.log(Object.values(areas));
console.log(Math.max.apply(null, Object.values(areas)));

26
06/part2.js Normal file
View File

@ -0,0 +1,26 @@
var input = require("fs").readFileSync("input.txt").toString();
var points = input.split("\n").filter(a=>a).map(a=>(a.split(",").map(b=>(parseInt(b)))));
var xmin = points.map(a=>(a[0])).sort((a,b)=>(a-b))[0];
var xmax = points.map(a=>(a[0])).sort((a,b)=>(b-a))[0];
var ymin = points.map(a=>(a[1])).sort((a,b)=>(a-b))[0];
var ymax = points.map(a=>(a[1])).sort((a,b)=>(b-a))[0];
var areas = {};
var count = 0;
var man = (xa, ya, xb, yb)=>(Math.abs(xa - xb) + Math.abs(ya - yb));
for(var x = xmin; x <= xmax; x++){
for(var y = ymin; y <= ymax; y++){
var dist = points.map(p=>(man(p[0], p[1], x, y))).reduce((a,b)=>(a+b));
//var dists = points.map(p=>(man(p[0], p[1], x, y)));
//console.log(x,y,dists,dist);
if(dist < 10000){
count++;
}
}
}
console.log(count);

1
08/input-test.txt Normal file
View File

@ -0,0 +1 @@
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2

1
08/input.txt Normal file

File diff suppressed because one or more lines are too long

28
08/part1.js Normal file
View File

@ -0,0 +1,28 @@
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
var nums = input.split(" ").map(a=>(parseInt(a)));
function parseTree(ints){
var numChildren = ints[0];
var numMeta = ints[1];
var children = [];
var i = 0;
while(numChildren > 0){
var childParsed = parseTree(ints.slice(2 + i));
i += childParsed[1].length;
children.push(childParsed[0]);
numChildren--;
}
return [{children: children, meta: ints.slice(2+i, 2+i+numMeta)}, ints.slice(0, 2+i+numMeta)]
}
function treeMetaTotal(tree){
var total = 0;
for(var child of tree.children){
total += treeMetaTotal(child);
}
total += tree.meta.reduce((a,b)=>(a+b));
return total;
}
console.log(treeMetaTotal(parseTree(nums)[0]));

29
08/part2.js Normal file
View File

@ -0,0 +1,29 @@
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
var nums = input.split(" ").map(a=>(parseInt(a)));
function parseTree(ints){
var numChildren = ints[0];
var numMeta = ints[1];
var children = [];
var i = 0;
while(numChildren > 0){
var childParsed = parseTree(ints.slice(2 + i));
i += childParsed[1].length;
children.push(childParsed[0]);
numChildren--;
}
return [{children: children, meta: ints.slice(2+i, 2+i+numMeta)}, ints.slice(0, 2+i+numMeta)]
}
function nodeValue(node){
var total = 0;
if(node.children.length){
return node.meta.map(a=>(a-1)).map(a=>(node.children[a])).filter(a=>(a)).map(n=>(nodeValue(n))).reduce((a,b)=>(a+b), 0);
}
else{
return node.meta.reduce((a,b)=>(a+b));
}
}
console.log(nodeValue(parseTree(nums)[0]));