diff --git a/2022/15/part1.js b/2022/15/part1.js index 72190fd..35851b7 100644 --- a/2022/15/part1.js +++ b/2022/15/part1.js @@ -11,7 +11,7 @@ const readInput = (...args) => readFile(...args), (lines) => lines.split("\n"), R.compact, // remove last line - R.map(line => line.match(/x=.*?y=\d+/g).map(str => + R.map(line => line.match(/x=-?.*?y=-?\d+/g).map(str => str.match(/-?\d+/g).map(x => parseInt(x))).map( coords => V(coords) ) diff --git a/2022/15/part2.js b/2022/15/part2.js index 10fc179..aeb3ead 100644 --- a/2022/15/part2.js +++ b/2022/15/part2.js @@ -1,4 +1,24 @@ import * as R from "remeda"; +import V from '@yanick/vyktor'; +export const findBeacon = max => entries => { -export default () => {}; + const nonGrata = R.uniqBy(R.flatten(entries), (x) => x.toString());; + + 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; + } + } +} + +export default findBeacon(4000000); diff --git a/2022/15/test.js b/2022/15/test.js index c843547..414fea6 100644 --- a/2022/15/test.js +++ b/2022/15/test.js @@ -2,7 +2,7 @@ import { test, expect, describe } from "vitest"; import { expectSolution } from "../01/main.js"; import part1, { sample, puzzleInput, unbeaconAtLine } from "./part1.js"; -import part2 from "./part2.js"; +import part2, { findBeacon } from "./part2.js"; describe("part 1", () => { test('readInput', () => { @@ -19,7 +19,10 @@ describe("part 1", () => { }); describe("part 2", () => { - test.todo("solution", () => { + test('sample', () => { + expect(findBeacon(20)(sample)).toEqual(56000011); + }); + test.only("solution", () => { expectSolution(part2(puzzleInput)).toEqual("TODO"); }); });