we can iterate just once!

This commit is contained in:
David 2024-12-04 22:48:53 -05:00
parent 9e99375349
commit e7e1b615ff

View File

@ -17,16 +17,12 @@ console.log(
.reduce(
(coll, curr, index, array) => {
// 2D string matching is a pain. Let's turn it into a 1D problem!
curr.split("").forEach((s, i) => {
// create the columns
curr.split("").forEach((s, i) => {
coll[i] += s;
});
// create the down-left-diagonals
curr.split("").forEach((s, i) => {
coll[i + array.length + index] += s;
});
// create the down-right-diagonals
curr.split("").forEach((s, i) => {
// create the left-diagonals
coll[array.length + index + i] += s;
// create the right-diagonals
coll[array.length * 4 + index - i - 2] += s;
});
// include the original row