Skip to content

Instantly share code, notes, and snippets.

@fersilva16
Created December 19, 2021 16:44
Show Gist options
  • Save fersilva16/16c7f36e35f57aa87663d770ad3ee843 to your computer and use it in GitHub Desktop.
Save fersilva16/16c7f36e35f57aa87663d770ad3ee843 to your computer and use it in GitHub Desktop.
Advent of Code 2021 - Day 05 A - TypeScript solution using mutable array
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