part 2, crummy but working
This commit is contained in:
parent
0a281c084c
commit
b3cacb15de
@ -21,8 +21,9 @@ export const puzzleInput = readInput(import.meta.url, "input");
|
||||
export const sample = readInput(import.meta.url, "sample");
|
||||
|
||||
/** @returns [number,number][] */
|
||||
const mergeRanges = ( ranges ) =>
|
||||
ranges.reduce( (accum,range) => {
|
||||
export const mergeRanges = ( ranges ) => {
|
||||
|
||||
return ranges.reduce( (accum,range) => {
|
||||
if( accum.length === 0 ) return [ range ];
|
||||
if( R.last( accum )[1] < range[0] ) {
|
||||
accum.push(range);
|
||||
@ -34,15 +35,14 @@ const mergeRanges = ( ranges ) =>
|
||||
}
|
||||
return accum;
|
||||
}, [] );
|
||||
}
|
||||
|
||||
const spy = passthru( x => console.log(x) );
|
||||
export const spy = passthru( x => console.log(x) );
|
||||
|
||||
const entriesInRange = (targetLine,entries) => {
|
||||
|
||||
entries = R.uniqBy(entries.map(([_,beacon])=>beacon).filter( beacon => beacon?.y === targetLine ), (x) => x.toString());
|
||||
|
||||
console.log(entries);
|
||||
|
||||
return range => {
|
||||
return entries.filter(
|
||||
entry => (entry.x >= range[0]) && (entry.x <= range[1])
|
||||
|
@ -1,23 +1,62 @@
|
||||
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 nonGrata = R.uniqBy(R.flatten(entries), (x) => x.toString());;
|
||||
const already = entries.flat()
|
||||
|
||||
const deadZones = R.map( entries, ([x,y]) => [ x, x.manhattanDistance(y) ] );
|
||||
|
||||
for ( let x =0; x <= max; x++ ) {
|
||||
for (let y = 0; y <= max; y++) {
|
||||
if( nonGrata.some( p => p.x===x && p.y===y ) ) continue;
|
||||
|
||||
const v = V(x,y);
|
||||
if( deadZones.some(
|
||||
d => d[0].manhattanDistance( v ) <= d[1]
|
||||
) ) continue;
|
||||
|
||||
return x * 4000000 + 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,8 @@ describe("part 2", () => {
|
||||
test('sample', () => {
|
||||
expect(findBeacon(20)(sample)).toEqual(56000011);
|
||||
});
|
||||
test.only("solution", () => {
|
||||
expectSolution(part2(puzzleInput)).toEqual("TODO");
|
||||
test("solution", () => {
|
||||
// 314 seconds!
|
||||
expectSolution(part2(puzzleInput)).toEqual(11756174628223);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user