Inspired by the connection rules from Calvin's Hobbies' question Make me an alphabet tree take a number of lines of letters as input and output the number of distinct connected trees it represents.
Letters connect to diagonally adjacent letters (never horizontally or vertically) and only where such a connection is available. Available connections correspond to the end of a line in the shape of the letter. For example, X can potentially connect at all four corners as they are all the ends of lines. W can only connect at the top left and top right corners, as the bottom left and bottom right corners are not ends of lines.
Example 1
Input:
XXX
XXX
XXX
Output:
2
The two trees (which do not form part of the output):
X X X
\ / / \
X X X
/ \ \ /
X X X
Example 2
Input:
AMF
QWK
Output:
3
The three trees (which do not form part of the output):
A F M
\ / \
W Q K
Rules
- The only valid characters are spaces, newlines, and the 23 upper case letters listed in Calvin's Hobbies' question: ACEFGHIJKLMNPQRSTUVWXYZ.
- The letters B, D and O are not valid.
- The output is undefined if the input contains invalid characters.
- A tree can be of any size, including just 1 letter and including over 23 letters.
- The same letter can appear an arbitrary number of times in a single tree.
- The input will not be more than 79 lines long.
- Each line (excluding newlines) will not be more than 79 characters long.
- The lines will not necessarily be the same length as each other.
- A tree is defined to be a set of connected letters. Since the connections are all diagonal this excludes letters which are horizontally or vertically adjacent from being part of the same tree.
- Input containing zero trees (for example, all spaces and newlines) gives output 0.
- To clarify, input and output are as defined on meta (so there is not already a variable containing the input).
This is code golf. The shortest correct code wins. Code length is measured in bytes.
If you have python 3.4, the alphabet tree counter outputs the number of connected trees in its input, and can be used to test your golfed code gives the same results. The alphabet tree checker confirms whether its input is a valid output for Calvin's Hobbies' original question, and outputs a list of the distinct trees in the input. This may be useful for visualising the trees if there is any dispute over the number of trees in a given input (although using it on large forests will give a lot of output - a visualisation of every single tree separately).
If the either of these is incorrect please let me know so I can fix it.
Table of connections.
1 is for upper left, 2 is for upper right, 3 lower left, 4 lower right.
A: 3 4
C: 2 4
E: 2 4
F: 2 3
G: 2
H: 1 2 3 4
I: 1 2 3 4
J: 1 3
K: 1 2 3 4
L: 1 4
M: 3 4
N: 2 3
P: 3
Q: 4
R: 3 4
S: 2 3
T: 1 2
U: 1 2
V: 1 2
W: 1 2
X: 1 2 3 4
Y: 1 2
Z: 1 4
Sample forest
Here is an example of a forest to test your code with. It has the maximum size of 79 x 79 and no spaces. The alphabet tree counter gives an output of 714. I don't recommend running the alphabet tree checker on it as it will try to output a separate visualisation of every single one of the 714 trees.
CKZNXXJEHCNHICKFHNAFZTMNXZKXSXAFHEKKFJYUZPCHRSNAUPEZKIHZJSKKPHQJYHKMXSHCXKEHCXQ
YSRCHHQLXXKKXITAAHSKMXISHIGFXMVXTZZKLXIXVHJYKTAHSHYPCFLHSWIHHCXXRXXJNKXXVVRNSKM
CZWJKKNIXXUHKXCKRTXXSVHQZERHXIXNHXPKELNKHXRHFKHIEPKUWKXKUPHEKKIKLLSFWUSLYEEXLSI
KHGURVKSXXKAGXIWKCSHFNVWWHNHNIVYKKAKZKFSKHCVRHUENHFIIKNUNICHYRKYXXHTLKYHPQKWHKI
KGAFRXJMKMJJSXCUTFAKLHHFXTIXKNHKKRHIZHXWRRXJNHXQEMKKXCNVHWFMPERJKKZLHIKIUZRKWLM
YRXXXCXXHVISVIYTHNHKSXKRXYHXUFYYKXTHXRYIIKHEWHXVZAZHTQKJAKKHXKKVCKJHWHIIZXXZZYK
FQLXHIHJFKSKWMTHXISAXTVIKKHTIXUAGUAJHHHHRVHXHRNWKXKUKWHVUJLKIJCWTLCHTXSYTKLTFWX
AEKNFXUXHXHICSNCYWNEVVALHXUSACWKRNJAKLLXKVJCZHSAKYHXLHHTHFKSMCGXHFHPKTHXTRYYWXK
HHHKHXGKFUHKSKHKKHHRHIKYNQSXMKQKIXHTXXHHIXCZIWGXKAHRXKRYXPXXXVSYIXWXVMUFYWHKINZ
XXWWHXUTXGKXFKZUTHHZPJGMAEHZVGHLEXHKKPXHZIVKFETHKXTIVKSMHWYEKHIMIHWEHKHIIJXXJZS
XHHXHRHKKXHHVXYZMNHIYHKKXXKAVWKUKEXYGGCHMLHFJCKXXXHEXGCZUKMHIXKVXKHZHFLCKXIPZHH
TCHAXXKHHNKKMKPRF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