This commit is contained in:
Yanick Champoux 2022-12-15 14:35:55 -05:00
parent f745366e8b
commit 562e2f4737
4 changed files with 87 additions and 0 deletions

44
2022/15/part1.js Normal file
View File

@ -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);

4
2022/15/part2.js Normal file
View File

@ -0,0 +1,4 @@
import * as R from "remeda";
export default () => {};

14
2022/15/sample Normal file
View File

@ -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

25
2022/15/test.js Normal file
View File

@ -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");
});
});