Browse Source

Move SumArray function to main common file

The function ended up not being used in day 9 part i, but it is still a
useful function, so I'll keep it in the common file.
ApisNecros 1 year ago
parent
commit
06594b985a
2 changed files with 11 additions and 5 deletions
  1. 1 5
      9/9_1.ts
  2. 10 0
      common.ts

+ 1 - 5
9/9_1.ts

@@ -1,4 +1,4 @@
-import { ArrayIsOnlyDuplicates, LoadInput } from "../common.ts";
+import { SumArray, ArrayIsOnlyDuplicates, LoadInput } from "../common.ts";
 import { FindDifferential, ParseInput, type Histogram } from "./9_common.ts";
 
 const tests = [
@@ -66,7 +66,3 @@ function PredictNextValue(histogram: Histogram): Histogram {
 
     return allDifferentials[0];
 }
-
-function SumArray(arr: number[]): number {
-    return arr.reduce((accumulator, value) => accumulator += value, 0);
-}

+ 10 - 0
common.ts

@@ -64,4 +64,14 @@ export function ArrayIsOnlyDuplicates(arr: Array<any>): boolean {
 	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);
 }