dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
98 lines
2.7 KiB
Markdown
98 lines
2.7 KiB
Markdown
---
|
|
id: 6a22d77ddf034bc4e35b1d56
|
|
title: "Challenge 349: Cell Signal"
|
|
challengeType: 29
|
|
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--
|
|
|
|
`find_signal([[0, 0, 1], [0, 1, 0], [0, 0, 1]])` should return `[1, 2]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(find_signal([[0, 0, 1], [0, 1, 0], [0, 0, 1]]), [1, 2])`)
|
|
}})
|
|
```
|
|
|
|
`find_signal([[0, 2, 0], [1, 0, 0], [0, 0, 1]])` should return `[2, 1]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(find_signal([[0, 2, 0], [1, 0, 0], [0, 0, 1]]), [2, 1])`)
|
|
}})
|
|
```
|
|
|
|
`find_signal([[0, 0, 2, 0], [0, 0, 0, 0], [2, 0, 0, 0], [0, 0, 0, 1]])` should return `[2, 2]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(find_signal([[0, 0, 2, 0], [0, 0, 0, 0], [2, 0, 0, 0], [0, 0, 0, 1]]), [2, 2])`)
|
|
}})
|
|
```
|
|
|
|
`find_signal([[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
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(find_signal([[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])`)
|
|
}})
|
|
```
|
|
|
|
`find_signal([[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
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(find_signal([[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--
|
|
|
|
```py
|
|
def find_signal(grid):
|
|
|
|
return grid
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
def find_signal(grid):
|
|
rows = len(grid)
|
|
cols = len(grid[0])
|
|
|
|
towers = []
|
|
for r in range(rows):
|
|
for c in range(cols):
|
|
if grid[r][c] != 0:
|
|
towers.append((r, c, grid[r][c]))
|
|
|
|
def is_valid(r, c, tr, tc, dist):
|
|
dr = abs(r - tr)
|
|
dc = abs(c - tc)
|
|
return (dr == dist or dc == dist) and (dr == 0 or dc == 0 or dr == dc)
|
|
|
|
for r in range(rows):
|
|
for c in range(cols):
|
|
if grid[r][c] != 0:
|
|
continue
|
|
if all(is_valid(r, c, tr, tc, dist) for tr, tc, dist in towers):
|
|
return [r, c]
|
|
```
|