This repository was archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
54 lines (45 loc) · 1.64 KB
/
helpers.ts
File metadata and controls
54 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Read the input file
export const readData = async function (filePath: string) {
const input = await fetch(filePath).then((response) => response.text());
return input;
};
// Splits the input into an array of strings
export const strInput = (data: string): string[] => data.split("\n");
export const splitOnSpaces = (data: string): string[] => data.split("\n\n");
// Splits a single string input into an array of numbers
export const numInput = (data: string): number[] =>
data.split("\n").map(Number);
// Creates an empty array of a given length
export function lenArray(num: number) {
return Array.from(Array(num).keys());
}
// removes all items from an array
export const clearArr = (arr: any) => arr.splice(0, arr.length);
// make a count object to count instances of an item
export const counts = (arr: any) =>
arr.reduce(function (allItems: any, item: any) {
if (item in allItems) {
allItems[item]++;
} else {
allItems[item] = 1;
}
return allItems;
}, {});
export function mean(array: number[]): number {
const sum: number = array.reduce((prev, curr) => prev + curr);
return Math.floor(sum / array.length);
}
export function median(array: number[]): number {
const sorted: number[] = array.sort((a, b) => a - b);
if (array.length % 2 === 0) {
return Math.floor(
sorted[array.length / 2 - 1] + sorted[array.length / 2] / 2
);
} else {
return Math.floor(sorted[array.length - 1 / 2]);
}
}
// get the triangular number sequence - the number of dots in a triangular pattern. Like a factorial, but for addition
export function triangleSequence(num: number): number {
return (num * (num + 1)) / 2;
}