--- id: 6a22d77ddf034bc4e35b1d56 title: "Challenge 349: Cell Signal" challengeType: 28 dashedName: challenge-349 --- # --description-- Given a grid containing three cell tower readings, determine the location of the phone. - Each cell in the grid is either `0` (no tower) or a positive integer representing the number of cells to the phone, measured in a straight line: horizontal, vertical, or diagonal. - Return the `[row, col]` of the cell that is the correct number of cells from all three towers. - There is always exactly one solution. # --hints-- `findSignal([[0, 0, 1], [0, 1, 0], [0, 0, 1]])` should return `[1, 2]`. ```js assert.deepEqual(findSignal([[0, 0, 1], [0, 1, 0], [0, 0, 1]]), [1, 2]); ``` `findSignal([[0, 2, 0], [1, 0, 0], [0, 0, 1]])` should return `[2, 1]`. ```js assert.deepEqual(findSignal([[0, 2, 0], [1, 0, 0], [0, 0, 1]]), [2, 1]); ``` `findSignal([[0, 0, 2, 0], [0, 0, 0, 0], [2, 0, 0, 0], [0, 0, 0, 1]])` should return `[2, 2]`. ```js assert.deepEqual(findSignal([[0, 0, 2, 0], [0, 0, 0, 0], [2, 0, 0, 0], [0, 0, 0, 1]]), [2, 2]); ``` `findSignal([[0, 3, 0, 0, 0], [0, 0, 0, 0, 2], [0, 0, 0, 0, 0], [4, 0, 0, 0, 0], [0, 0, 0, 0, 0]])` should return `[3, 4]`. ```js assert.deepEqual(findSignal([[0, 3, 0, 0, 0], [0, 0, 0, 0, 2], [0, 0, 0, 0, 0], [4, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), [3, 4]); ``` `findSignal([[3, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 2]])` should return `[3, 3]`. ```js assert.deepEqual(findSignal([[3, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 2]]), [3, 3]); ``` # --seed-- ## --seed-contents-- ```js function findSignal(grid) { return grid; } ``` # --solutions-- ```js function findSignal(grid) { const rows = grid.length; const cols = grid[0].length; const towers = []; for (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { if (grid[r][c] !== 0) towers.push([r, c, grid[r][c]]); } } function isValid(r, c, tr, tc, dist) { const dr = Math.abs(r - tr); const dc = Math.abs(c - tc); return (dr === dist || dc === dist) && (dr === 0 || dc === 0 || dr === dc); } for (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { if (grid[r][c] !== 0) continue; if (towers.every(([tr, tc, dist]) => isValid(r, c, tr, tc, dist))) { return [r, c]; } } } } ```