feat: advent of code day two
Node.js CI / Lint and Test (push) Successful in 47s

This commit is contained in:
2025-12-02 12:06:20 -08:00
parent f4425c3fbf
commit c728436ed7
3 changed files with 113 additions and 0 deletions
@@ -0,0 +1 @@
19391-47353,9354357-9434558,4646427538-4646497433,273-830,612658-674925,6639011-6699773,4426384-4463095,527495356-527575097,22323258-22422396,412175-431622,492524-611114,77-122,992964846-993029776,165081-338962,925961-994113,7967153617-7967231799,71518058-71542434,64164836-64292066,4495586-4655083,2-17,432139-454960,4645-14066,6073872-6232058,9999984021-10000017929,704216-909374,48425929-48543963,52767-94156,26-76,1252-3919,123-228
+34
View File
@@ -0,0 +1,34 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, it, expect } from "vitest";
import { partOne, partTwo } from "./main.js";
const inputOne = `11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124`;
describe("partOne", () => {
it("should return the correct result", async() => {
expect(partOne(inputOne)).toBe(1_227_775_554);
const bigInput = await readFile(
join(import.meta.dirname, "inputOne.txt"),
"utf8",
);
expect(partOne(bigInput)).toBe(24_747_430_309);
});
});
describe("partTwo", () => {
it("should return the correct result", async() => {
expect(partTwo(inputOne)).toBe(4_174_379_265);
const bigInput = await readFile(
join(import.meta.dirname, "inputOne.txt"),
"utf8",
);
expect(partTwo(bigInput)).toBe(30_962_646_823);
});
});
+78
View File
@@ -0,0 +1,78 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
/**
* Determine the sum of invalid IDs in a range.
* An ID is invalid if it consists of two pairs of repeating digits.
* @param start - The start of the range.
* @param end - The end of the range.
* @returns The sum of invalid IDs in the range.
* @example 998-1012 has one invalid ID, 1010.
* @example 1188511880-1188511890 has one invalid ID, 1188511888.
*/
const sumInvalidIds = (start: number, end: number): number => {
let sum = 0;
for (let index = start; index <= end; index = index + 1) {
const id = index.toString();
const { length } = id;
if (length % 2 !== 0) {
continue;
}
const half = length / 2;
const firstHalf = id.slice(0, half);
const secondHalf = id.slice(half);
if (firstHalf === secondHalf) {
sum = sum + index;
}
}
return sum;
};
const sumLongerInvalidIds = (start: number, end: number): number => {
let sum = 0;
for (let index = start; index <= end; index = index + 1) {
const id = index.toString();
// Regex to detect if string consists ONLY of two or more groups of repeated digits, like 121212.
if (/^(?<name>[\d\s]+)\1+$/.test(id)) {
sum = sum + index;
}
}
return sum;
};
/**
* Part one of the Advent of Code 2025 day 2 challenge.
* @param input - The input string.
* @returns The sum of invalid IDs.
* @see https://adventofcode.com/2025/day/2
*/
const partOne = (input: string): number => {
const idRanges: Array<[number, number]> = input.split(",").map((range) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We know the input is valid.
return range.split("-").map(Number) as [number, number];
});
return idRanges.reduce((sum, [ start, end ]) => {
return sum + sumInvalidIds(start, end);
}, 0);
};
/**
* Part two of the Advent of Code 2025 day 1 challenge.
* @param input - The input string.
* @returns The number of times the position passed through 0.
* @see https://adventofcode.com/2025/day/1
*/
const partTwo = (input: string): number => {
const idRanges: Array<[number, number]> = input.split(",").map((range) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We know the input is valid.
return range.split("-").map(Number) as [number, number];
});
return idRanges.reduce((sum, [ start, end ]) => {
return sum + sumLongerInvalidIds(start, end);
}, 0);
};
export { partOne, partTwo };