77 lines
1.9 KiB
JavaScript
77 lines
1.9 KiB
JavaScript
|
const {
|
||
|
Glib,
|
||
|
BNMath: Math,
|
||
|
fn,
|
||
|
Vector,
|
||
|
twodee: { Rectangle },
|
||
|
} = require('../../lib');
|
||
|
|
||
|
const parse = (input) =>
|
||
|
Glib.fromLines(input).map((i) => Vector.fromString(`(${i})`));
|
||
|
|
||
|
const SIZE_CONSTRAINT = 30n;
|
||
|
|
||
|
const calculateRegion = (input) => {
|
||
|
const [minX, maxX] = input.glib.map((i) => i.v[0]).minMax();
|
||
|
const [minY, maxY] = input.glib.map((i) => i.v[1]).minMax();
|
||
|
|
||
|
return {
|
||
|
startX: minX - SIZE_CONSTRAINT,
|
||
|
stopX: maxX + SIZE_CONSTRAINT,
|
||
|
startY: minY - SIZE_CONSTRAINT,
|
||
|
stopY: maxY + SIZE_CONSTRAINT,
|
||
|
};
|
||
|
};
|
||
|
|
||
|
module.exports = {
|
||
|
'1': (input) => {
|
||
|
input = parse(input).array;
|
||
|
|
||
|
const { startX, stopX, startY, stopY } = calculateRegion(input);
|
||
|
|
||
|
const { points, infinite } = Rectangle.fromXXYY(
|
||
|
startX,
|
||
|
stopX,
|
||
|
startY,
|
||
|
stopY,
|
||
|
)
|
||
|
.allPoints()
|
||
|
.reduce(
|
||
|
(acc, pt) => {
|
||
|
const distances = input.glib.map((p) => [p, p.distance(pt)]).array;
|
||
|
const [closest, distance] = distances.glib.minScore();
|
||
|
const count = distances.glib.filter(([pt, v]) => v === distance)
|
||
|
.length;
|
||
|
if (count === 1n) {
|
||
|
acc.points.set(pt, closest);
|
||
|
if (
|
||
|
pt.v[0] === startX ||
|
||
|
pt.v[0] === stopX ||
|
||
|
pt.v[1] === startY ||
|
||
|
pt.v[1] === stopY
|
||
|
) {
|
||
|
acc.infinite.add(closest);
|
||
|
}
|
||
|
}
|
||
|
return acc;
|
||
|
},
|
||
|
{ points: new Map(), infinite: new Set() },
|
||
|
);
|
||
|
|
||
|
return input.glib
|
||
|
.filterBySet(infinite, true)
|
||
|
.lookupInMap(points.glibValues.frequency())
|
||
|
.max();
|
||
|
},
|
||
|
'2': (input) => {
|
||
|
input = parse(input).array;
|
||
|
|
||
|
const { startX, stopX, startY, stopY } = calculateRegion(input);
|
||
|
|
||
|
return Rectangle.fromXXYY(startX, stopX, startY, stopY)
|
||
|
.allPoints()
|
||
|
.filter((pt) => input.glib.map((k) => pt.distance(k)).sum() < 10000n)
|
||
|
.length;
|
||
|
},
|
||
|
};
|