2018 d6 js

This commit is contained in:
Tristan 2018-12-06 07:59:41 +00:00
parent 12f9f51921
commit 6d0db850b4
5 changed files with 111 additions and 0 deletions

50
2018/6/input.txt Normal file
View file

@ -0,0 +1,50 @@
84, 212
168, 116
195, 339
110, 86
303, 244
228, 338
151, 295
115, 49
161, 98
60, 197
40, 55
55, 322
148, 82
86, 349
145, 295
243, 281
91, 343
280, 50
149, 129
174, 119
170, 44
296, 148
152, 160
115, 251
266, 281
269, 285
109, 242
136, 241
236, 249
338, 245
71, 101
254, 327
208, 231
289, 184
282, 158
352, 51
326, 230
88, 240
292, 342
352, 189
231, 141
280, 350
296, 185
226, 252
172, 235
137, 161
207, 90
101, 133
156, 234
241, 185

35
2018/6/part1.js Normal file
View file

@ -0,0 +1,35 @@
const fs = require('fs');
let input = fs.readFileSync('input.txt', 'utf-8').split('\n').map(v => v.split(',').map(Number));
let minX = input.reduce((a, b) => Math.min(a[0] || a, b[0]));
let minY = input.reduce((a, b) => Math.min(a[1] || a, b[1]));
let maxX = input.reduce((a, b) => Math.max(a[0] || a, b[0]));
let maxY = input.reduce((a, b) => Math.max(a[1] || a, b[1]));
values = {};
for (let y = minY; y <= maxY; y++) {
for (let x = minX; x <= maxX; x++) {
let vs = input.map(v => Math.abs(v[0] - x) + Math.abs(v[1] - y));
let min = Math.min.apply(null, vs);
let index;
if (vs.filter(a => a == min).length == 1) {
index = vs.indexOf(min).toString();
values[index] = (values[index] || 0) + 1;
}
}
}
for (edge of [minY, maxY]) {
for (let x = minX; x <= maxX; x++) {
let vs = input.map(v => Math.abs(v[0] - x) + Math.abs(v[1] - edge));
index = vs.indexOf(Math.min(...vs)).toString();
values[index] = Infinity
}
}
for (edge of [minX, maxX]) {
for (let y = minY; y <= maxY; y++) {
let vs = input.map(v => Math.abs(v[0] - edge) + Math.abs(v[1] - y));
index = vs.indexOf(Math.min(...vs)).toString();
values[index] = Infinity
}
}
finite = Object.values(values).filter(v => v != Infinity)
console.log(finite.sort((a, b) => a < b)) // for some reason it doesn't sort them properly, but the answer will be somewhere near the top.

19
2018/6/part2.js Normal file
View file

@ -0,0 +1,19 @@
const fs = require('fs');
let input = fs.readFileSync('input.txt', 'utf-8').split('\n').map(v => v.split(',').map(Number));
let minX = input.reduce((a, b) => Math.min(a[0] || a, b[0]));
let minY = input.reduce((a, b) => Math.min(a[1] || a, b[1]));
let maxX = input.reduce((a, b) => Math.max(a[0] || a, b[0]));
let maxY = input.reduce((a, b) => Math.max(a[1] || a, b[1]));
let amt = 0
for (let y = minY; y <= maxY; y++) {
for (let x = minX; x <= maxX; x++) {
let total = input.reduce((a, b) => (typeof a == 'number' ? a : dist([x, y], a)) + dist([x, y], b));
amt += total < 10000 ? 1 : 0;
}
}
function dist(c1, c2) {
return Math.abs(c1[0] - c2[0]) + Math.abs(c1[1] - c2[1])
}
console.log(amt)

6
2018/6/test.txt Normal file
View file

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

View file

@ -17,6 +17,7 @@ It's a fun challenge from [here](https://adventofcode.com).
* [3](https://adventofcode.com/2018/day/3) js * * * [3](https://adventofcode.com/2018/day/3) js * *
* [4](https://adventofcode.com/2018/day/4) js * * * [4](https://adventofcode.com/2018/day/4) js * *
* [5](https://adventofcode.com/2018/day/5) js * * * [5](https://adventofcode.com/2018/day/5) js * *
* [6](https://adventofcode.com/2018/day/6) js * *
# Languages Used # Languages Used
* Python * Python