feat: advent of code day one
Node.js CI / Lint and Test (push) Has been cancelled

This commit is contained in:
2025-12-02 11:43:39 -08:00
parent 6bd6acdfce
commit f4425c3fbf
3 changed files with 4603 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+44
View File
@@ -0,0 +1,44 @@
/**
* @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 = `L68
L30
R48
L5
R60
L55
L1
L99
R14
L82
R0`;
describe("partOne", () => {
it("should return the correct result", async() => {
expect(partOne(inputOne)).toBe(3);
const bigInput = await readFile(
join(import.meta.dirname, "inputOne.txt"),
"utf8",
);
expect(partOne(bigInput)).toBe(1086);
});
});
describe("partTwo", () => {
it("should return the correct result", async() => {
expect(partTwo(inputOne)).toBe(6);
const bigInput = await readFile(
join(import.meta.dirname, "inputOne.txt"),
"utf8",
);
expect(partTwo(bigInput)).toBe(6268);
});
});
+72
View File
@@ -0,0 +1,72 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
const normalizePosition = (value: number): number => {
const modulus = value % 100;
return (modulus + 100) % 100;
};
const countZeroHits = (start: number, end: number): number => {
if (end === start) {
return 0;
}
if (end > start) {
return Math.floor(end / 100) - Math.floor(start / 100);
}
return Math.floor((start - 1) / 100) - Math.floor((end - 1) / 100);
};
/**
* Part one of the Advent of Code 2025 day 1 challenge.
* @param input - The input string.
* @returns The number of times the position is 0.
* @see https://adventofcode.com/2025/day/1
*/
const partOne = (input: string): number => {
const instructions = input.split("\n").map((line) => {
return [ line[0], line.slice(1) ];
});
let position = 50;
let count = 0;
for (const instruction of instructions) {
const [ direction, distance ] = instruction;
if (direction === "L") {
position = position - Number(distance);
} else {
position = position + Number(distance);
}
position = normalizePosition(position);
if (position === 0) {
count = count + 1;
}
}
return count;
};
/**
* 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 instructions = input.split("\n").map((line) => {
return [ line[0], line.slice(1) ];
});
let position = 50;
let count = 0;
for (const instruction of instructions) {
const [ direction, distance ] = instruction;
const delta = direction === "L"
? -Number(distance)
: Number(distance);
const endPosition = position + delta;
count = count + countZeroHits(position, endPosition);
position = normalizePosition(endPosition);
}
return count;
};
export { partOne, partTwo };