Skip to content

Instantly share code, notes, and snippets.

@YannickFricke
Created December 5, 2024 00:05
Show Gist options
  • Save YannickFricke/ebde119d0b93b8f0cd4a47d8b355b8db to your computer and use it in GitHub Desktop.
Save YannickFricke/ebde119d0b93b8f0cd4a47d8b355b8db to your computer and use it in GitHub Desktop.
List<FoundMatch> findMatches(List<List<String>> lines, String wordToFind) {
final foundMatches = <FoundMatch>[];
for (var lineIndex = 0; lineIndex < lines.length; lineIndex++) {
final row = lines[lineIndex];
for (var columnIndex = 0; columnIndex < row.length; columnIndex++) {
final currentCharacter = lines[lineIndex][columnIndex];
if (currentCharacter != wordToFind[0]) {
continue;
}
if (lineIndex >= wordToFind.length - 1) {
// Check if we can find the word to the top direction
final foundWord = lines
.skip(lineIndex + 1 - wordToFind.length)
.take(wordToFind.length)
.map((line) => line[columnIndex])
.toList()
.reversed
.join("");
if (foundWord == wordToFind) {
foundMatches.add(
FoundMatch(
Direction.top,
lineIndex,
columnIndex,
lineIndex + 1 - wordToFind.length,
columnIndex,
),
);
}
}
if (lineIndex >= wordToFind.length - 1 &&
columnIndex <= row.length - wordToFind.length) {
// Check if we can find the word to the top-right direction
final foundWord = calculateDiagonalIndexes(
lineIndex, columnIndex, wordToFind.length, -1, 1)
.map((tuple) => lines[tuple.$1][tuple.$2])
.join("");
if (foundWord == wordToFind) {
foundMatches.add(
FoundMatch(
Direction.topLeft,
lineIndex,
columnIndex,
lineIndex + 1 - wordToFind.length,
columnIndex + wordToFind.length - 1,
),
);
}
}
if (columnIndex <= row.length - wordToFind.length) {
// Check if the word fits the right side
final foundWord =
row.skip(columnIndex).take(wordToFind.length).join("");
if (foundWord == wordToFind) {
foundMatches.add(
FoundMatch(
Direction.right,
lineIndex,
columnIndex,
lineIndex,
columnIndex + wordToFind.length,
),
);
}
}
if (lineIndex <= lines.length - wordToFind.length &&
columnIndex <= row.length - wordToFind.length) {
// Check if we can find the word to the bottom-right direction
final foundWord = calculateDiagonalIndexes(
lineIndex,
columnIndex,
wordToFind.length,
1,
1,
).map((tuple) => lines[tuple.$1][tuple.$2]).join("");
if (foundWord == wordToFind) {
foundMatches.add(
FoundMatch(
Direction.bottomRight,
lineIndex,
columnIndex,
lineIndex + wordToFind.length - 1,
columnIndex + wordToFind.length - 1,
),
);
}
}
if (lineIndex <= lines.length - wordToFind.length) {
// Check if we can find the word to the bottom direction
final foundWord = lines
.skip(lineIndex)
.take(wordToFind.length)
.map((row) => row[columnIndex])
.join("");
if (foundWord == wordToFind) {
foundMatches.add(
FoundMatch(
Direction.bottom,
lineIndex,
columnIndex,
lineIndex + wordToFind.length - 1,
columnIndex,
),
);
}
}
if (lineIndex <= lines.length - wordToFind.length &&
columnIndex >= wordToFind.length - 1) {
// Check if we can find the word to the bottom-left direction
final foundWord = calculateDiagonalIndexes(
lineIndex, columnIndex, wordToFind.length, 1, -1)
.map((tuple) => lines[tuple.$1][tuple.$2])
.join("");
if (foundWord == wordToFind) {
foundMatches.add(
FoundMatch(
Direction.bottomLeft,
lineIndex,
columnIndex,
lineIndex + wordToFind.length - 1,
columnIndex + 1 - wordToFind.length,
),
);
}
}
if (columnIndex >= wordToFind.length - 1) {
// Check if we can find the word to the left direction
final foundWord = row
.skip(columnIndex - wordToFind.length + 1)
.take(wordToFind.length)
.toList()
.reversed
.join("");
if (foundWord == wordToFind) {
foundMatches.add(
FoundMatch(
Direction.left,
lineIndex,
columnIndex + 1,
lineIndex,
columnIndex + 1 - wordToFind.length,
),
);
}
}
if (columnIndex >= wordToFind.length - 1 &&
lineIndex >= wordToFind.length - 1) {
// Check if we can find the word to the top-left direction
final foundWord = calculateDiagonalIndexes(
lineIndex, columnIndex, wordToFind.length, -1, -1)
.map((tuple) => lines[tuple.$1][tuple.$2])
.join();
if (foundWord == wordToFind) {
foundMatches.add(
FoundMatch(
Direction.topLeft,
lineIndex,
columnIndex,
lineIndex + 1 - wordToFind.length,
columnIndex + 1 - wordToFind.length,
),
);
}
}
}
}
return foundMatches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment