Skip to content

WIP: 2018 - Day 23 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added 2018/23/README.md
Empty file.
1,002 changes: 1,002 additions & 0 deletions 2018/23/input.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions 2018/23/manhatten.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const m = (a, b) => {
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]) + Math.abs(a[2] - b[2]);
}

module.exports = m;
21 changes: 21 additions & 0 deletions 2018/23/part-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const distance = require('manhattan');
const input = require('./input');
const cloned_input = JSON.parse(JSON.stringify(input));

cloned_input.sort((a, b) => {
if (a.r < b.r) return -1;
else if (a.r > b.r) return 1;
else return 0;
});

let largest = cloned_input[cloned_input.length - 1];

let in_range = 0;
for (let i = 0; i < input.length - 1; i++) {
let bot = input[i];
if (distance(bot.pos, largest.pos) <= largest.r) {
in_range++;
}
}

console.log(in_range);
65 changes: 65 additions & 0 deletions 2018/23/part-two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const distance = require('manhattan');
const input = require('./input');
// const input = require('./sample-input2');
const cloned_input = JSON.parse(JSON.stringify(input));

xs = cloned_input.map(p => p.pos[0]);
ys = cloned_input.map(p => p.pos[1]);
zs = cloned_input.map(p => p.pos[2]);

const sortNum = (a, b) => {
if (a < b) return -1;
else if (a > b) return 1;
else return 0;
};

xs.sort(sortNum);
ys.sort(sortNum);
zs.sort(sortNum);

const min_x = xs[0];
const max_x = xs[xs.length - 1];

const min_y = ys[0];
const max_y = ys[ys.length - 1];

const min_z = zs[0];
const max_z = zs[zs.length - 1];

let best_coord = {
coord: [null, null, null],
inRangeOf: -1
};

console.log(`Running ${((max_x - min_x) * (max_y - min_y) * (max_z - min_z)).toLocaleString()}`)



for (let x = min_x; x < max_x; x++) {
for (let y = min_y; y < max_y; y++) {
for (let z = min_z; z < max_z; z++) {
let coord = [x,y,z];

let inRangeOf = 0;
input.forEach(bot => {
let d = distance(coord, bot.pos);
if (d <= bot.r) {
inRangeOf++;
}
});


if (inRangeOf > best_coord.inRangeOf) {
console.log(`New best [${x}, ${y}, ${z}], in range of ${inRangeOf}`);

best_coord.coord[0] = x;
best_coord.coord[1] = y;
best_coord.coord[2] = z;
best_coord.inRangeOf = inRangeOf;
}
}
}
}

console.log(best_coord);
console.log(distance(best_coord.coord, [0,0,0]))
11 changes: 11 additions & 0 deletions 2018/23/sample-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = [
{ pos: [0, 0, 0], r: 4 },
{ pos: [1, 0, 0], r: 1 },
{ pos: [4, 0, 0], r: 3 },
{ pos: [0, 2, 0], r: 1 },
{ pos: [0, 5, 0], r: 3 },
{ pos: [0, 0, 3], r: 1 },
{ pos: [1, 1, 1], r: 1 },
{ pos: [1, 1, 2], r: 1 },
{ pos: [1, 3, 1], r: 1 },
];
8 changes: 8 additions & 0 deletions 2018/23/sample-input2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = [
{ pos: [10, 12, 12], r: 2 },
{ pos: [12, 14, 12], r: 2 },
{ pos: [16, 12, 12], r: 4 },
{ pos: [14, 14, 14], r: 6 },
{ pos: [50, 50, 50], r: 200 },
{ pos: [10, 10, 10], r: 5 },
];