From c728436ed7c4ff06cb2a10f500771b0d0dbbd2ce Mon Sep 17 00:00:00 2001 From: Naomi Carrigan Date: Tue, 2 Dec 2025 12:06:20 -0800 Subject: [PATCH] feat: advent of code day two --- src/advent-of-code/2025/day2/inputOne.txt | 1 + src/advent-of-code/2025/day2/main.spec.ts | 34 ++++++++++ src/advent-of-code/2025/day2/main.ts | 78 +++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/advent-of-code/2025/day2/inputOne.txt create mode 100644 src/advent-of-code/2025/day2/main.spec.ts create mode 100644 src/advent-of-code/2025/day2/main.ts diff --git a/src/advent-of-code/2025/day2/inputOne.txt b/src/advent-of-code/2025/day2/inputOne.txt new file mode 100644 index 0000000..76abbd8 --- /dev/null +++ b/src/advent-of-code/2025/day2/inputOne.txt @@ -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 \ No newline at end of file diff --git a/src/advent-of-code/2025/day2/main.spec.ts b/src/advent-of-code/2025/day2/main.spec.ts new file mode 100644 index 0000000..16ffa20 --- /dev/null +++ b/src/advent-of-code/2025/day2/main.spec.ts @@ -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); + }); +}); diff --git a/src/advent-of-code/2025/day2/main.ts b/src/advent-of-code/2025/day2/main.ts new file mode 100644 index 0000000..bf07479 --- /dev/null +++ b/src/advent-of-code/2025/day2/main.ts @@ -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 (/^(?[\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 };