19
2
This is the second in a series of Island Golf challenges. Previous challenge
Two hermits have arrived on a desert island. Since they came seeking solitude, they wish to live as far away from each other as possible. Where should they build their huts to maximize the walking distance between them?
Input
Your input will be a rectangular grid consisting of two characters, representing land and water. In the examples below, land is #
and water is .
, but you may substitute any two distinct characters you wish.
...........
...##......
..#####....
..#######..
.#########.
...#######.
...#####.#.
....####...
...........
There will always be at least two land tiles. The land tiles will all be contiguous (i.e. there's only one island). The water tiles will also be contiguous (i.e. there are no lakes). The outer border of the grid will all be water tiles. Land tiles will not be connected diagonally: i.e., you will never see something like
....
.#..
..#.
....
Output
Your code must output the same grid, with two hut locations marked on it. In the examples below, the hut locations are marked with X, but you may substitute any character as long as it is distinct from your land and water characters.
The hut locations must be two land tiles, chosen so as to maximize the walking distance between them. We define walking distance as the length of the shortest path, entirely on land, between the two points. Land tiles are considered adjacent horizontally or vertically, but not diagonally.
A possible solution for the above island:
...........
...X#......
..#####....
..#######..
.#########.
...#######.
...#####.X.
....####...
...........
The walking distance between these two points is 11, which is the greatest distance between any two points on this island. There is another distance-11 solution:
...........
...##......
..X####....
..#######..
.#########.
...#######.
...#####.X.
....####...
...........
Details
Your solution may be a full program or a function. Any of the default input and output methods are acceptable.
Your input and output may be a multiline string, a list of strings, or a 2D array/nested list of characters/single-character strings. Your output may (optionally) have a single trailing newline. As mentioned above, you may use any three distinct characters in place of #.X
(please specify in your submission which characters you're using).
Test cases
A. Islands with unique hut placements:
....
.##.
....
....
.XX.
....
......
......
..##..
...#..
......
......
......
......
..X#..
...X..
......
......
........
.#####..
.##..##.
.#..###.
.##..##.
........
........
.#####..
.##..##.
.#..###.
.#X..#X.
........
.........
.#####.#.
.#...#.#.
.#.###.#.
.#.....#.
.#######.
.........
.........
.#####.X.
.#...#.#.
.#.X##.#.
.#.....#.
.#######.
.........
B. Example of an island with multiple possible solutions:
........
....##..
...####.
..###...
.#####..
.#####..
..##....
........
Possible outputs:
........
....#X..
...####.
..###...
.#####..
.X####..
..##....
........
........
....#X..
...####.
..###...
.#####..
.#####..
..X#....
........
........
....##..
...###X.
..###...
.#####..
.X####..
..##....
........
........
....##..
...###X.
..###...
.#####..
.#####..
..X#....
........
This is code-golf: the shortest code in each language wins.
2These are great little challenges (I especially like not having to do any bounds checking!): looking forward to the next one! – VisualMelon – 2017-03-27T21:23:25.190
walking distance is manhattan distance? – Display Name – 2017-05-28T18:42:46.767
@SargeBorsch Closely related, but not always the same. Manhattan distance is just Δx + Δy, but walking distance may be longer because you can't walk across ocean tiles. (See the last example in section 'A', for example. The Manhattan distance between the two X's is 6, but the walking distance--following the spiral--is 22.) – DLosc – 2017-05-30T05:51:44.143