11
You have a little robot with four distance sensors. It knows the layout of a room, but it has no sense of orientation other than being able to lock onto the grid orientation. You want to be able to find out where the robot is based on the readings, but it can be ambiguous because of the limited sensors.
Challenge Explanation
You will be given a room layout and four clockwise distance readings giving the number of cells between you and a wall. There can be walls in the middle of the room and the edges of the grid are also walls. The robot cannot be placed on top of a wall.
Your objective is to list all of the locations within the room that the robot could be in that would give the given readings. Keep in mind that the robot has no sense of orientation (other than being locked to 90 degree angles on the grid- i.e. the robot will never be oriented diagonally or some other skew angle), so a reading of [1, 2, 3, 4], for example, is the same as reading [3, 4, 1, 2].
Examples
For these examples, cell coordinates will be given as 0-indexed (x, y) pairs from the top-left cell. Readings will be given in clockwise order in a square bracketed list. Layouts will use pound signs for walls and other characters (usually dots) to represent empty cells.
Case 1
. . . .
. . . .
. . # .
. . . .
- [1, 0, 2, 3] ==> (1, 0), (3, 1)
- [0, 0, 3, 3] ==> (0, 0), (3, 0), (0, 3), (3, 3)
- [2, 1, 1, 0] ==> (0, 2), (2, 1)
- [1, 1, 2, 2] ==> (1, 1)
Case 2
# a . # a .
a # . . # a
. . # . . #
# . . # . .
a # . . # a
. a # . a #
- [0, 0, 1, 1] ==> every position on the grid that is a dot
- [1, 0, 0, 0] ==> all of the a's on the grid
Case 3
.
- [0, 0, 0, 0] ==> (0, 0)
Case 4
. # #
. . .
- [1, 2, 0, 0] ==> (0, 1)
- [0, 1, 2, 0] ==> (0, 1)
- [0, 0, 1, 0] ==> (0, 0)
- [1, 0, 1, 0] ==> (1, 1)
- [0, 1, 0, 1] ==> (1, 1)
Case 5
. # . .
. . . .
. . # .
. . . .
- [2, 1, 1, 0] ==> (0, 2), (2, 1)
- [0, 2, 2, 1] ==> (1, 1)
- [1, 0, 2, 2] ==> (1, 1)
- [0, 3, 0, 0] ==> (0, 0)
- [1, 0, 1, 1] ==> (1, 2)
Other Rules
- Input may be in any convenient format. Input is a grid of walls and spaces and a list of four distances in clockwise order.
- Output may either be a list of all cells that satisfy the reading or a modified version of the grid showing which cells satisfy the reading. The exact format of the output doesn't matter as long as it is reasonable and consistent. Valid output formats include, but are not limited to:
- Printing a line for each cell coordinate as an ordered pair
- Printing the grid with
.
,#
, and!
for space, walls, and possible locations, respectively. - Returning a list of ordered pairs
- Returning a list of indexes
- Returning a list of lists using different values for spaces, walls, and possible locations
- Return/print a matrix of 0s and 1s, using 1s to represent cells where the reading would occur. (It is not necessary to include walls)
- Once again, this list is not exhaustive, so other representations are valid as long as they are consistent and show every possible valid location in a grid or list. If you are unsure, leave a comment and I will be happy to clarify.
- You may assume that a reading corresponds to at least one location on the grid.
- You may assume that the input grid is at least 1x1 in size and has at least one empty space.
- You may assume that the input grid is no larger than 256 cells in each dimension.
- You may assume that the input grid is always a perfect rectangle and not jagged.
- There is no penalty or bonus if your program happens to give sane outputs for invalid inputs.
- This is code golf, so shortest code wins.
The testcases for
Case 5
don't seem quite right. I get(0,2),(2,1)
,(1,3)
,(1,3)
, andnothing
. – TFeld – 2019-01-29T19:33:46.103@TFeld Thanks. Fixed. – Beefster – 2019-01-29T21:17:06.210
1@Arnauld Seems reasonable to me. I'll add that to the already non-exhaustive list. – Beefster – 2019-01-30T18:25:41.107