1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import * as fs from "fs";
- import * as readline from "readline";
- import * as util from "util";
- export async function LoadInput(dayNumber: number): Promise<string[]> {
- const contents = [];
- const stream = fs.createReadStream(`./inputs/${dayNumber}.txt`, "utf8");
- const rl = readline.createInterface({
- input: stream,
- crlfDelay: Infinity,
- });
- for await (const line of rl) {
- contents.push(line);
- }
- return contents;
- }
- /**
- * Inspect a variable in the console
- *
- * @param {any} value A value to inspect
- */
- export function Inspect(value: any): void {
- console.log(util.inspect(value, { breakLength: Infinity, colors: true, depth: 256 }));
- }
- /**
- * Deep clone any input
- *
- * @param {any} value A value to clone
- * @returns A clone of the input
- */
- export function DeepClone(value: any): any {
- return JSON.parse(JSON.stringify(value));
- }
- /**
- * Extracts all numbers from a string
- *
- * If there were no numbers in the string, then an empty array is returned.
- *
- * Numbers in the output are in the order they appeared in in the input
- *
- * @param {string} str A string
- * @returns {number[]} An array of numbers
- */
- export function ExtractNumbers(str: string): number[] {
- return str.split(/[^\-0-9]/).filter((n) => n.length).map((n) => Number(n));
- }
- /**
- * Check if an array only contains one unique value
- *
- * @see https://stackoverflow.com/a/14438954/2708601
- * @param arr An array of any type
- * @returns Whether the array only contains duplicates or not
- */
- export function ArrayIsOnlyDuplicates(arr: Array<any>): boolean {
- if (arr.length === 1) { return true; }
- return arr.filter((val, idx, array) => {
- return array.indexOf(val) === idx;
- }).length == 1;
- }
- /**
- * Sum all values in an array
- *
- * @param arr The array to sum
- * @returns The sum of all elements in the array
- */
- export function SumArray(arr: number[]): number {
- return arr.reduce((accumulator, value) => accumulator += value, 0);
- }
|