adventofcode/2022/15/part2.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-12-15 19:35:55 +00:00
import * as R from "remeda";
2023-11-30 15:13:16 +00:00
import V from "@yanick/vyktor";
2022-12-15 19:35:55 +00:00
2023-11-30 15:13:16 +00:00
import { spy, mergeRanges } from "./part1.js";
2022-12-15 19:35:55 +00:00
2023-11-30 15:13:16 +00:00
const unbeaconAtLine = (max, targetLine, already) => (entries) =>
R.pipe(
2022-12-18 21:06:07 +00:00
entries,
2023-11-30 15:13:16 +00:00
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];
}),
2022-12-18 21:06:07 +00:00
//spy,
(ranges) => [
2023-11-30 15:13:16 +00:00
...ranges,
...already.filter((v) => v.y === targetLine).map((v) => [v.x, v.x]),
2022-12-18 21:06:07 +00:00
],
2023-11-30 15:13:16 +00:00
(ranges) =>
ranges.sort((a, b) => {
return a[0] - b[0] || a[1] - b[1];
}),
2022-12-18 21:06:07 +00:00
// spy,
2023-11-30 15:13:16 +00:00
R.map((range) => [Math.max(0, range[0]), Math.min(max, range[1])]),
mergeRanges
2022-12-18 21:06:07 +00:00
// spy,
2023-11-30 15:13:16 +00:00
// R.sumBy( range => range[1] - range[0] + 1 - entriesInRange(targetLine,entries)(range) )
);
2022-12-18 18:39:06 +00:00
2023-11-30 15:13:16 +00:00
export const findBeacon = (max) => (entries) => {
const already = entries.flat();
2022-12-18 18:39:06 +00:00
2023-11-30 15:13:16 +00:00
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);
}
2022-12-18 21:06:07 +00:00
2023-11-30 15:13:16 +00:00
// return 'bob';
2022-12-18 21:06:07 +00:00
2023-11-30 15:13:16 +00:00
// return x * 4000000 + y;
}
};
2022-12-18 18:39:06 +00:00
export default findBeacon(4000000);