Created
May 30, 2024 11:21
-
-
Save qoobes/ddac6af47827b8576f9a06ad2925f96a to your computer and use it in GitHub Desktop.
Sa rekurzijom je zivot laksi
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
const words = ["fav", "tutor", "pear", "tua", "boo", "prrv", "ttpv"]; | |
const BaseBoard = [ | |
["t", "u", "a", "p"], | |
["f", "t", "o", "r"], | |
["a", "v", "p", "r"], | |
["l", "l", "e", "v"], | |
]; | |
function solve() { | |
let solution: string[] = []; | |
for (const word of words) { | |
for (let i = 0; i < BaseBoard.length; i++) { | |
for (let j = 0; j < BaseBoard[i].length; j++) { | |
console.log("looking at base letter", i, j, BaseBoard[i][j]); | |
if (BaseBoard[i][j] === word[0] && lookAround([i, j], word.slice(1), BaseBoard)) { | |
console.log("pushing to solution", word); | |
solution.push(word); | |
i = BaseBoard.length - 1; | |
} | |
} | |
} | |
} | |
console.log(solution); | |
} | |
function lookAround(idx: [number, number], word: string, board: string[][]): boolean { | |
if (word === "") return true; | |
const directions = [ | |
[-1, 0], | |
[1, 0], | |
[0, -1], | |
[0, 1], | |
[-1, -1], | |
[-1, 1], | |
[1, -1], | |
[1, 1], | |
]; | |
for (const [dx, dy] of directions) { | |
console.log("diff", dx, dy); | |
const x = idx[0] + dx; | |
const y = idx[1] + dy; | |
console.log("letter", x, y, board[x]?.[y]); | |
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] !== word[0]) continue; | |
if (lookAround([x, y], word.slice(1), board)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
solve(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment