Easy As ABC, also known as "End View", is a puzzle where you are given an empty grid with letters around it; you must partially fill in the grid so that exactly one of each letter is in every row and column; in addition, letters at the end of a row (or column) must be the first letter visible in that row (or column) from that direction. Your goal in this code golf will be to solve an Easy As ABC puzzle.
For example, here is an Easy As ABC puzzle from this year's MIT Mystery Hunt using the letters MIC:
The solution is:
(Sorry about the artifacts on the Cs; I tried to edit out the irrelevant information from the rest of the puzzle.)
I/O
Input will be an array of strings or a string possibly with delimiters. It will start in the top left corner and go clockwise. For instance, the above puzzle could be input like this:
".CMM.M|....IM|.....I|C.ICI."
Output should be the solved grid, with or without the border. It can be as an array of chars, array of strings, or any other convenient format. The same "blank" character must be accepted as input and displayed as output, but that blank character may be anything. If they are single strings, both input and output must have the same separator (between sides for input and rows for output) or no separator at all.
For unsolvable puzzles, you must output something that is not mistakable for a solution. You may assume that no puzzle has more than one solution.
You must allow any number of letters and any size grid; all used letters will appear in the border of the grid.
This is code-golf: as usual, shortest code wins!
Test Cases
"T.AA..|.T.TSS|..TST.|A...SS"
"R.RU..|B.B..B|.UR.UB|UR..B."
"N...NK|E.NK.K|..KK..|....EK"
"CA..DBD|.B..CC.|.D.DEB.|DB.A..A"
"...DDEBE|DC..EBBD|BA..ABF.|E..FECDE"