123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import fs from "node:fs";
- /**
- * Load the input for a day's challenge
- *
- * @param {string|number} dayNumber The number of the day to load. A string for a file path can be provided here too.
- * @param {Function} parsingFunction A function to parse the input to a usable format
- * @param {boolean} literalPath If true, the {@link dayNumber} value should be used as a file path
- *
- * @returns {unknown}
- */
- export function loadInput(dayNumber, parsingFunction, literalPath = false) {
- let filePath = literalPath ? dayNumber : `./inputs/day_${dayNumber}.input`;
- const data = fs.readFileSync(filePath, "utf8");
- return typeof parsingFunction == "function" ? parsingFunction(data) : data;
- }
- /**
- * Comparator for sorting numbers into ascending order
- *
- * @param {number} a
- * @param {number} b
- *
- * @returns {-1|0|1}
- */
- export function numericAscSort(a, b) {
- return clamp(a - b);
- }
- /**
- * Convert a string to an integer
- *
- * Does no checks to make sure the value is actually a number-like value
- *
- * @param {string} string
- *
- * @returns {number}
- */
- export function strToInt(string) {
- return parseInt(string, 10);
- }
- /**
- * Clamp a number between two other numbers
- *
- * @param {number} value The value to be clamped
- * @param {number} min The minimum value
- * @param {number} max The maximum value
- *
- * @returns {number} The clamped value
- */
- export function clamp(value, min = -1, max = 1) {
- return Math.max(min, Math.min(max, value));
- }
- /**
- * Deep clone a value
- *
- * @param {any} value
- *
- * @returns {any}
- */
- export function deepClone(value) {
- return JSON.parse(JSON.stringify(value));
- }
|