From b3cacb15ded479d79e4f0c9d9ce17d53fe0c753e Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 18 Dec 2022 16:06:07 -0500 Subject: [PATCH] part 2, crummy but working --- 2022/15/part1.js | 10 ++++---- 2022/15/part2.js | 65 ++++++++++++++++++++++++++++++++++++++---------- 2022/15/test.js | 5 ++-- 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/2022/15/part1.js b/2022/15/part1.js index 35851b7..a196927 100644 --- a/2022/15/part1.js +++ b/2022/15/part1.js @@ -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]) diff --git a/2022/15/part2.js b/2022/15/part2.js index aeb3ead..543b6bf 100644 --- a/2022/15/part2.js +++ b/2022/15/part2.js @@ -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; + 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; } } diff --git a/2022/15/test.js b/2022/15/test.js index 414fea6..e53af8b 100644 --- a/2022/15/test.js +++ b/2022/15/test.js @@ -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); }); });