1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
words = StringSplit[StringReplace[Import["p098_words.txt"], "\"" -> ""], ","];
wordpairs= SortBy[Select[GatherBy[words, Sort@Characters@# &], Length@# > 1 &],100 - StringLength@First@# &];
f[w1_, w2_] :=
Module[{}, l = StringLength@w1; ret = {};
For[i = Ceiling@Sqrt[10^(l - 1)], i <= Floor@Sqrt[10^l - 1], i++,
{d, w1d, w2d} = {IntegerDigits[i^2], Characters@w1, Characters@w2};
assc = <||>;
flag = True;
For[j = 1, j <= Length@d, j++,
If[KeyExistsQ[assc, w1d[[j]]],
If[assc[w1d[[j]]] != d[[j]], flag = False; Break[]],
If[MemberQ[Values@assc, d[[j]]], flag = False; Break[],
assc[w1d[[j]]] = d[[j]]]]
];
If[flag, w2i = FromDigits[w2d /. Normal[assc]];
If[IntegerLength@w2i == l && IntegerQ@Sqrt@w2i, ret = {ret, assc}]]
]; Flatten@ret];
Take[f@@@wordpairs,-20]
{f["POST", "SPOT", "STOP"], {}, {<|"B" -> 1, "O" -> 7, "A" -> 6, "R" -> 8, "D" -> 9|>}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} }
18769
|