diff --git a/2022/15/part1.js b/2022/15/part1.js new file mode 100644 index 0000000..64861c7 --- /dev/null +++ b/2022/15/part1.js @@ -0,0 +1,44 @@ +import * as R from "remeda"; + +import { readFile } from "../05/part1.js"; + +import V from '@yanick/vyktor'; + +const readInput = (...args) => + R.pipe( + readFile(...args), + (lines) => lines.split("\n"), + R.compact, // remove last line + R.map(line => line.match(/x=.*?y=\d+/g).map(str => + str.match(/-?\d+/g).map(x => parseInt(x))).map( + coords => V(coords) + ) + )); + +export const puzzleInput = readInput(import.meta.url, "input"); +export const sample = readInput(import.meta.url, "sample"); + +export const unbeaconAtLine = targetLine => R.createPipe( + 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 R.range( + e[0].x - l, + e[0].x + l, + ) } + ), + R.flatten, + R.uniq, + x => x.length + + ); + +export default unbeaconAtLine(2000000); diff --git a/2022/15/part2.js b/2022/15/part2.js new file mode 100644 index 0000000..10fc179 --- /dev/null +++ b/2022/15/part2.js @@ -0,0 +1,4 @@ +import * as R from "remeda"; + + +export default () => {}; diff --git a/2022/15/sample b/2022/15/sample new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/2022/15/sample @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 diff --git a/2022/15/test.js b/2022/15/test.js new file mode 100644 index 0000000..c843547 --- /dev/null +++ b/2022/15/test.js @@ -0,0 +1,25 @@ +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"; + +describe("part 1", () => { + test('readInput', () => { + expect(sample[0][0].toArray()).toEqual([2, 18]); + }); + + test('sample', () => { + expect(unbeaconAtLine(10)(sample)).toEqual(26); + }); + + test("solution", () => { + expectSolution(part1(puzzleInput)).toEqual(5525990); + }); +}); + +describe("part 2", () => { + test.todo("solution", () => { + expectSolution(part2(puzzleInput)).toEqual("TODO"); + }); +});