Created
December 19, 2021 16:44
-
-
Save fersilva16/16c7f36e35f57aa87663d770ad3ee843 to your computer and use it in GitHub Desktop.
Advent of Code 2021 - Day 05 A - TypeScript solution using mutable array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function day05a(xs: [number, number, number, number][], matrix: number[][]) { | |
xs.forEach((x) => { | |
const [x1, y1, x2, y2] = x; | |
if (x1 == x2 || y1 == y2) { | |
for (let yi = Math.min(y1, y2); yi <= Math.max(y1, y2); yi++) { | |
for (let xi = Math.min(x1, x2); xi <= Math.max(x1, x2); xi++) | |
m[yi][xi]++; | |
} | |
} | |
}); | |
return matrix | |
.map((r) => r.map((x) => Number(x > 1)).reduce((a, b) => a + b, 0)) | |
.reduce((a, b) => a + b, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment