Skip to content

Commit

Permalink
2017: Extract sum()
Browse files Browse the repository at this point in the history
  • Loading branch information
jp7677 committed Dec 15, 2024
1 parent f8eab0f commit 352e4cf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion 2017/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[*]
[*.ts]
charset = utf-8
insert_final_newline = true
end_of_line = lf
Expand Down
6 changes: 2 additions & 4 deletions 2017/src/day02.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -38,7 +36,7 @@ describe("day 02", () => {

return n1 > n2 ? n1 / n2 : n2 / n1;
})
.reduce((acc, it) => acc + it);
.sum();

expect(checksum).toBe(294);
});
Expand Down
5 changes: 5 additions & 0 deletions 2017/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare global {
interface Array<T> {
max(): number;
min(): number;
sum(): number;
}
}

Expand All @@ -28,3 +29,7 @@ Array.prototype.min = function () {
else return acc;
});
};

Array.prototype.sum = function () {
return this.reduce((acc, it) => acc + it);
};

0 comments on commit 352e4cf

Please sign in to comment.