From 352e4cff9b30b475c6d7617967a3a8d74c46135d Mon Sep 17 00:00:00 2001 From: Jens Peters Date: Sun, 15 Dec 2024 18:44:49 +0100 Subject: [PATCH] 2017: Extract sum() --- 2017/.editorconfig | 2 +- 2017/src/day02.spec.ts | 6 ++---- 2017/src/util.ts | 5 +++++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/2017/.editorconfig b/2017/.editorconfig index 3920760..4471cc5 100644 --- a/2017/.editorconfig +++ b/2017/.editorconfig @@ -1,4 +1,4 @@ -[*] +[*.ts] charset = utf-8 insert_final_newline = true end_of_line = lf diff --git a/2017/src/day02.spec.ts b/2017/src/day02.spec.ts index e09bc25..44a7f0c 100644 --- a/2017/src/day02.spec.ts +++ b/2017/src/day02.spec.ts @@ -9,9 +9,7 @@ describe("day 02", () => { (line) => line.split(RegExp("\t")).map((c) => parseInt(c)), ); - const checksum = spreadsheet - .map((row) => row.max() - row.min()) - .reduce((acc, it) => acc + it); + const checksum = spreadsheet.map((row) => row.max() - row.min()).sum(); expect(checksum).toBe(45158); }); @@ -38,7 +36,7 @@ describe("day 02", () => { return n1 > n2 ? n1 / n2 : n2 / n1; }) - .reduce((acc, it) => acc + it); + .sum(); expect(checksum).toBe(294); }); diff --git a/2017/src/util.ts b/2017/src/util.ts index 80112d9..6d0336b 100644 --- a/2017/src/util.ts +++ b/2017/src/util.ts @@ -12,6 +12,7 @@ declare global { interface Array { max(): number; min(): number; + sum(): number; } } @@ -28,3 +29,7 @@ Array.prototype.min = function () { else return acc; }); }; + +Array.prototype.sum = function () { + return this.reduce((acc, it) => acc + it); +};