48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
import * as R from "remeda";
|
|
import V from "@yanick/vyktor";
|
|
|
|
import { spy, mergeRanges } from "./part1.js";
|
|
|
|
const unbeaconAtLine = (max, targetLine, already) => (entries) =>
|
|
R.pipe(
|
|
entries,
|
|
R.map(([x, y]) => [x, x.manhattanDistance(y)]),
|
|
R.filter((e) => e[0].y - e[1] <= targetLine && e[0].y + e[1] >= targetLine),
|
|
R.map((e) => {
|
|
const l = e[1] - Math.abs(targetLine - e[0].y);
|
|
|
|
return [e[0].x - l, e[0].x + l];
|
|
}),
|
|
//spy,
|
|
(ranges) => [
|
|
...ranges,
|
|
...already.filter((v) => v.y === targetLine).map((v) => [v.x, v.x]),
|
|
],
|
|
(ranges) =>
|
|
ranges.sort((a, b) => {
|
|
return a[0] - b[0] || a[1] - b[1];
|
|
}),
|
|
// spy,
|
|
R.map((range) => [Math.max(0, range[0]), Math.min(max, range[1])]),
|
|
mergeRanges
|
|
// spy,
|
|
// R.sumBy( range => range[1] - range[0] + 1 - entriesInRange(targetLine,entries)(range) )
|
|
);
|
|
|
|
export const findBeacon = (max) => (entries) => {
|
|
const already = entries.flat();
|
|
|
|
for (let y = 0; y <= max; y++) {
|
|
const ranges = unbeaconAtLine(max, y, already)(entries);
|
|
if (ranges.length > 1) {
|
|
return y + 4000000 * (ranges[0][1] + 1);
|
|
}
|
|
|
|
// return 'bob';
|
|
|
|
// return x * 4000000 + y;
|
|
}
|
|
};
|
|
|
|
export default findBeacon(4000000);
|