adventofcode/2022/15/part2.js

64 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-12-15 19:35:55 +00:00
import * as R from "remeda";
2022-12-18 18:39:06 +00:00
import V from '@yanick/vyktor';
2022-12-15 19:35:55 +00:00
2022-12-18 21:06:07 +00:00
import {
spy,
mergeRanges
} from './part1.js';
2022-12-15 19:35:55 +00:00
2022-12-18 21:06:07 +00:00
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);
2022-12-18 18:39:06 +00:00
2022-12-18 21:06:07 +00:00
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) )
);
2022-12-18 18:39:06 +00:00
2022-12-18 21:06:07 +00:00
export const findBeacon = max => entries => {
2022-12-18 18:39:06 +00:00
2022-12-18 21:06:07 +00:00
const already = entries.flat()
2022-12-18 18:39:06 +00:00
2022-12-18 21:06:07 +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 18:39:06 +00:00
}
2022-12-18 21:06:07 +00:00
// return 'bob';
// return x * 4000000 + y;
2022-12-18 18:39:06 +00:00
}
}
export default findBeacon(4000000);