Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*
Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.
For example: With Astanian cells marked A
, Blandic cells marked B
and trench cells marked +
(the map frames are only for clarity):
┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘
Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.
You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.
Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.
Test cases
[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]
[
"AAA",
"AAA",
"BBB"
]
becomes:
[
"AAA",
"AAA",
"+++"
]
[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]
* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!
<sup><sub><sup><sub><sup><sub><sup><sub>
:-P \$\endgroup\$ – Luis Mendo Jan 16 '19 at 19:44pass
The plans to build a border trench lead to a government shutdown and nothing happens. \$\endgroup\$ – TheEspinosa Jan 16 '19 at 21:43