JavaScript - not pretty and will think about how to make more elegant 😊 . I can move to codepen or something if this is way to big of a blurb
const getAllIdx = (arr, val) => { let indexes = [], i = -1; while ((i = arr.indexOf(val, i+1)) != -1){ indexes.push(i); } return indexes; } const intersect = (a, b) => { const setA = new Set(a); const setB = new Set(b); const intersection = new Set([...setA].filter(x => setB.has(x))); return Array.from(intersection); } const range = (start, stop, step) => { const result = []; for (let i = start; i <= stop; i += step) { result.push(i); } return result; } const canBuildWord = (height, [first, second, ...rest], used=[]) => { let res = []; if (second === undefined) return true; for(let i = 0; i < first.length; i++) { nextUsed = [...used, first[i]]; let val = first[i]; ajacentIdx = [val-height, val+height]; const rightBound = range(height-1, (height-1) + (height * (height-1)), height); const leftBound = range(0, height * height - 1, height); if (!rightBound.includes(val)) { ajacentIdx.push(val+1, val-height+1, val+height+1); } if (!leftBound.includes(val)) { ajacentIdx.push(val-1, val-height-1, val+height-1); } ajacentIdx = ajacentIdx.filter(x => !nextUsed.includes(x)); let intersection = intersect(ajacentIdx, second); if (intersection.length === 0) { res.push(false); } else { for(let j = 0; j < intersection.length; j++) { res.push(canBuildWord(height, [intersection, ...rest], [...nextUsed, intersection[j]])); } } } return res.some(x => x); } const checkWord = (board, guess) => { const chars = guess.split(''); const flat = [].concat.apply([], board); const startingPoints =[]; for (let i = 0; i < chars.length; i++) { startingPoints.push(getAllIdx(flat, chars[i])); } const missingLetter = startingPoints.filter(points => points.length === 0).length !== 0; if (missingLetter) return false; return canBuildWord(board.length, startingPoints); }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript - not pretty and will think about how to make more elegant 😊 . I can move to codepen or something if this is way to big of a blurb