part 2, crummy but working

This commit is contained in:
Yanick Champoux 2022-12-18 16:06:07 -05:00
parent 0a281c084c
commit b3cacb15de
3 changed files with 60 additions and 20 deletions

View File

@ -21,8 +21,9 @@ export const puzzleInput = readInput(import.meta.url, "input");
export const sample = readInput(import.meta.url, "sample"); export const sample = readInput(import.meta.url, "sample");
/** @returns [number,number][] */ /** @returns [number,number][] */
const mergeRanges = ( ranges ) => export const mergeRanges = ( ranges ) => {
ranges.reduce( (accum,range) => {
return ranges.reduce( (accum,range) => {
if( accum.length === 0 ) return [ range ]; if( accum.length === 0 ) return [ range ];
if( R.last( accum )[1] < range[0] ) { if( R.last( accum )[1] < range[0] ) {
accum.push(range); accum.push(range);
@ -34,15 +35,14 @@ const mergeRanges = ( ranges ) =>
} }
return accum; return accum;
}, [] ); }, [] );
}
const spy = passthru( x => console.log(x) ); export const spy = passthru( x => console.log(x) );
const entriesInRange = (targetLine,entries) => { const entriesInRange = (targetLine,entries) => {
entries = R.uniqBy(entries.map(([_,beacon])=>beacon).filter( beacon => beacon?.y === targetLine ), (x) => x.toString()); entries = R.uniqBy(entries.map(([_,beacon])=>beacon).filter( beacon => beacon?.y === targetLine ), (x) => x.toString());
console.log(entries);
return range => { return range => {
return entries.filter( return entries.filter(
entry => (entry.x >= range[0]) && (entry.x <= range[1]) entry => (entry.x >= range[0]) && (entry.x <= range[1])

View File

@ -1,23 +1,62 @@
import * as R from "remeda"; import * as R from "remeda";
import V from '@yanick/vyktor'; 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 => { 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++) { for (let y = 0; y <= max; y++) {
if( nonGrata.some( p => p.x===x && p.y===y ) ) continue; const ranges = unbeaconAtLine(max,y,already)(entries)
if( ranges.length > 1 ) {
const v = V(x,y); return y + 4000000 * (ranges[0][1] +1);
if( deadZones.some(
d => d[0].manhattanDistance( v ) <= d[1]
) ) continue;
return x * 4000000 + y;
} }
// return 'bob';
// return x * 4000000 + y;
} }
} }

View File

@ -22,7 +22,8 @@ describe("part 2", () => {
test('sample', () => { test('sample', () => {
expect(findBeacon(20)(sample)).toEqual(56000011); expect(findBeacon(20)(sample)).toEqual(56000011);
}); });
test.only("solution", () => { test("solution", () => {
expectSolution(part2(puzzleInput)).toEqual("TODO"); // 314 seconds!
expectSolution(part2(puzzleInput)).toEqual(11756174628223);
}); });
}); });