part 2
This commit is contained in:
parent
26f9e6f512
commit
d9bf1d7d21
@ -2,7 +2,7 @@ import * as R from "remeda";
|
|||||||
import fs from "fs-extra";
|
import fs from "fs-extra";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
const readFile = (year, day, file) =>
|
export const readFile = (year, day, file) =>
|
||||||
fs.readFileSync(path.join(year, day, file), "utf8");
|
fs.readFileSync(path.join(year, day, file), "utf8");
|
||||||
|
|
||||||
export const sample = readFile("2022", "03", "sample");
|
export const sample = readFile("2022", "03", "sample");
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
import * as R from "remeda";
|
import * as R from "remeda";
|
||||||
|
|
||||||
import { readFile } from '../03/part1.js';
|
import { readFile } from "../03/part1.js";
|
||||||
|
|
||||||
export const sample = readFile("2022", "04", "sample");
|
export const sample = readFile("2022", "04", "sample");
|
||||||
export const puzzleInput = readFile("2022", "04", "input");
|
export const puzzleInput = readFile("2022", "04", "input");
|
||||||
|
|
||||||
|
const rangeLength = ([a, b]) => b - a + 1;
|
||||||
|
|
||||||
const rangeLength = ([a,b]) => b-a+1;
|
const isContainedBy = ([a1, a2], [b1, b2]) => a1 >= b1 && a2 <= b2;
|
||||||
|
|
||||||
const isContainedBy = ( [a1,a2],[b1,b2] ) =>
|
|
||||||
( (a1 >= b1) && (a2 <= b2) );
|
|
||||||
|
|
||||||
export const solutionPart1 = R.createPipe(
|
export const solutionPart1 = R.createPipe(
|
||||||
(text) => text.split("\n"),
|
(text) => text.split("\n"),
|
||||||
R.filter(R.identity),
|
R.filter(R.identity),
|
||||||
R.map( line => line.split(',').map( range => range.split('-').map( x => parseInt(x) ) ) ),
|
R.map((line) =>
|
||||||
R.map( R.sortBy( x => rangeLength(x) ) ),
|
line.split(",").map((range) => range.split("-").map((x) => parseInt(x)))
|
||||||
R.countBy( ([a,b]) => isContainedBy(a,b) )
|
),
|
||||||
|
R.map(R.sortBy((x) => rangeLength(x))),
|
||||||
|
R.countBy(([a, b]) => isContainedBy(a, b))
|
||||||
);
|
);
|
||||||
|
@ -1,2 +1,13 @@
|
|||||||
import * as R from "remeda";
|
import * as R from "remeda";
|
||||||
|
|
||||||
|
const overlapsWith = ([a1, a2], [b1, b2]) => a2 >= b1;
|
||||||
|
|
||||||
|
export const solutionPart2 = R.createPipe(
|
||||||
|
(text) => text.split("\n"),
|
||||||
|
R.filter(R.identity),
|
||||||
|
R.map((line) =>
|
||||||
|
line.split(",").map((range) => range.split("-").map((x) => parseInt(x)))
|
||||||
|
),
|
||||||
|
R.map(R.sortBy(([x]) => x)),
|
||||||
|
R.countBy(([a, b]) => overlapsWith(a, b))
|
||||||
|
);
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
import { test, expect, describe } from "vitest";
|
import { test, describe } from "vitest";
|
||||||
|
|
||||||
import { expectSolution } from "../01/main.js";
|
import { expectSolution } from "../01/main.js";
|
||||||
import {
|
import { solutionPart1, puzzleInput, sample } from "./part1.js";
|
||||||
solutionPart1,
|
|
||||||
puzzleInput,
|
|
||||||
sample,
|
|
||||||
} from "./part1.js";
|
|
||||||
import { solutionPart2 } from "./part2.js";
|
import { solutionPart2 } from "./part2.js";
|
||||||
|
|
||||||
describe("part 1", () => {
|
describe("part 1", () => {
|
||||||
@ -13,12 +9,15 @@ describe("part 1", () => {
|
|||||||
expectSolution(solutionPart1(sample)).toEqual(2);
|
expectSolution(solutionPart1(sample)).toEqual(2);
|
||||||
});
|
});
|
||||||
test("solution", () => {
|
test("solution", () => {
|
||||||
expectSolution(solutionPart1(puzzleInput)).toEqual('TODO');
|
expectSolution(solutionPart1(puzzleInput)).toEqual(605);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("part 2", () => {
|
describe("part 2", () => {
|
||||||
test.todo("solution", () => {
|
test("sample", () => {
|
||||||
expectSolution(solutionPart2(puzzleInput)).toEqual('TODO');
|
expectSolution(solutionPart2(sample)).toEqual(4);
|
||||||
|
});
|
||||||
|
test("solution", () => {
|
||||||
|
expectSolution(solutionPart2(puzzleInput)).toEqual(914);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user