From fec5b2ebac1b025e1fe8b90a9238a1b203d5fd36 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Fri, 7 Apr 2017 12:58:16 +0300 Subject: [PATCH] 8-word anagrams performance --- README.md | 2 +- dotnet/WhiteRabbit/Flattener.cs | 186 ++++++++---------- .../PrecomputedPermutationsGenerator.cs | 2 +- 3 files changed, 83 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index 5ff8624..60613ce 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Number of words|Time to check all anagrams no longer than that|Time to solve "ea 5|46s|||1.1s|1,348,876,896 6|34 minutes|||15s|58,837,302,096 7|11 hours (?)|45s|6.5s|2 minutes|1,108,328,708,976 -8|||||12,089,249,231,856 +8||4 minutes|27s|11 minutes|12,089,249,231,856 9|||||88,977,349,731,696 10|||||482,627,715,786,096 11|||||2,030,917,440,675,696 diff --git a/dotnet/WhiteRabbit/Flattener.cs b/dotnet/WhiteRabbit/Flattener.cs index a35cc36..6c17811 100644 --- a/dotnet/WhiteRabbit/Flattener.cs +++ b/dotnet/WhiteRabbit/Flattener.cs @@ -26,140 +26,114 @@ private static IEnumerable Flatten3(T[][] phrase) { foreach (var item0 in phrase[0]) - { - foreach (var item1 in phrase[1]) + foreach (var item1 in phrase[1]) + foreach (var item2 in phrase[2]) + yield return new T[] { - foreach (var item2 in phrase[2]) - { - yield return new T[] - { - item0, - item1, - item2, - }; - } - } - } + item0, + item1, + item2, + }; } // Fast hard-coded implementation for 4 words private static IEnumerable Flatten4(T[][] phrase) { foreach (var item0 in phrase[0]) - { - foreach (var item1 in phrase[1]) + foreach (var item1 in phrase[1]) + foreach (var item2 in phrase[2]) + foreach (var item3 in phrase[3]) + yield return new T[] { - foreach (var item2 in phrase[2]) - { - foreach (var item3 in phrase[3]) - { - yield return new T[] - { - item0, - item1, - item2, - item3, - }; - } - } - } - } + item0, + item1, + item2, + item3, + }; } // Fast hard-coded implementation for 5 words private static IEnumerable Flatten5(T[][] phrase) { foreach (var item0 in phrase[0]) - { - foreach (var item1 in phrase[1]) + foreach (var item1 in phrase[1]) + foreach (var item2 in phrase[2]) + foreach (var item3 in phrase[3]) + foreach (var item4 in phrase[4]) + yield return new T[] { - foreach (var item2 in phrase[2]) - { - foreach (var item3 in phrase[3]) - { - foreach (var item4 in phrase[4]) - { - yield return new T[] - { - item0, - item1, - item2, - item3, - item4, - }; - } - } - } - } - } + item0, + item1, + item2, + item3, + item4, + }; } // Fast hard-coded implementation for 6 words private static IEnumerable Flatten6(T[][] phrase) { foreach (var item0 in phrase[0]) - { - foreach (var item1 in phrase[1]) + foreach (var item1 in phrase[1]) + foreach (var item2 in phrase[2]) + foreach (var item3 in phrase[3]) + foreach (var item4 in phrase[4]) + foreach (var item5 in phrase[5]) + yield return new T[] { - foreach (var item2 in phrase[2]) - { - foreach (var item3 in phrase[3]) - { - foreach (var item4 in phrase[4]) - { - foreach (var item5 in phrase[5]) - { - yield return new T[] - { - item0, - item1, - item2, - item3, - item4, - item5, - }; - } - } - } - } - } - } + item0, + item1, + item2, + item3, + item4, + item5, + }; } // Fast hard-coded implementation for 7 words private static IEnumerable Flatten7(T[][] phrase) { foreach (var item0 in phrase[0]) - { - foreach (var item1 in phrase[1]) + foreach (var item1 in phrase[1]) + foreach (var item2 in phrase[2]) + foreach (var item3 in phrase[3]) + foreach (var item4 in phrase[4]) + foreach (var item5 in phrase[5]) + foreach (var item6 in phrase[6]) + yield return new T[] { - foreach (var item2 in phrase[2]) - { - foreach (var item3 in phrase[3]) - { - foreach (var item4 in phrase[4]) - { - foreach (var item5 in phrase[5]) - { - foreach (var item6 in phrase[6]) - { - yield return new T[] - { - item0, - item1, - item2, - item3, - item4, - item5, - item6, - }; - } - } - } - } - } - } - } + item0, + item1, + item2, + item3, + item4, + item5, + item6, + }; + } + + // Fast hard-coded implementation for 7 words + private static IEnumerable Flatten8(T[][] phrase) + { + foreach (var item0 in phrase[0]) + foreach (var item1 in phrase[1]) + foreach (var item2 in phrase[2]) + foreach (var item3 in phrase[3]) + foreach (var item4 in phrase[4]) + foreach (var item5 in phrase[5]) + foreach (var item6 in phrase[6]) + foreach (var item7 in phrase[7]) + yield return new T[] + { + item0, + item1, + item2, + item3, + item4, + item5, + item6, + item7, + }; } public static IEnumerable Flatten(T[][] wordVariants) @@ -176,6 +150,8 @@ return Flatten6(wordVariants); case 7: return Flatten7(wordVariants); + case 8: + return Flatten8(wordVariants); default: return FlattenAny(ImmutableStack.Create(wordVariants)).Select(words => words.ToArray()); } diff --git a/dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs b/dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs index 3389a32..b2c9f66 100644 --- a/dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs +++ b/dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs @@ -5,7 +5,7 @@ internal static class PrecomputedPermutationsGenerator { - private static int[][][] Permutations { get; } = Enumerable.Range(0, 8).Select(GeneratePermutations).ToArray(); + private static int[][][] Permutations { get; } = Enumerable.Range(0, 9).Select(GeneratePermutations).ToArray(); private static long[] PermutationsNumbers { get; } = GeneratePermutationsNumbers().Take(19).ToArray();