Code cleanup + comments

rx
Inga 🏳‍🌈 7 years ago
parent 957d53fa97
commit ed9967e51c
  1. 4
      WhiteRabbit/PrecomputedPermutationsGenerator.cs
  2. 2
      WhiteRabbit/Program.cs
  3. 4
      WhiteRabbit/StringsProcessor.cs
  4. 13
      WhiteRabbit/VectorsProcessor.cs

@ -3,7 +3,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
internal class PrecomputedPermutationsGenerator internal static class PrecomputedPermutationsGenerator
{ {
private static PermutationsGenerator.Permutation[] Permutations1 { get; } = PermutationsGenerator.HamiltonianPermutations(1).ToArray(); private static PermutationsGenerator.Permutation[] Permutations1 { get; } = PermutationsGenerator.HamiltonianPermutations(1).ToArray();
@ -17,7 +17,7 @@
public static IEnumerable<PermutationsGenerator.Permutation> HamiltonianPermutations(int n) public static IEnumerable<PermutationsGenerator.Permutation> HamiltonianPermutations(int n)
{ {
switch(n) switch (n)
{ {
case 1: case 1:
return Permutations1; return Permutations1;

@ -31,7 +31,7 @@
{ {
if (expectedHashesAsVectors.Contains(result.Item2)) if (expectedHashesAsVectors.Contains(result.Item2))
{ {
Console.WriteLine("Found phrase: " + result.Item1); Console.WriteLine($"Found phrase: {result.Item1}");
} }
} }
} }

@ -37,7 +37,7 @@
// converting sequences of vectors to the sequences of words... // converting sequences of vectors to the sequences of words...
var anagramsWords = sums var anagramsWords = sums
.Select(sum => ImmutableStack.Create(sum.Select(vector => formattedWords[vector]).ToArray())) .Select(sum => ImmutableStack.Create(sum.Select(vector => formattedWords[vector]).ToArray()))
.SelectMany(Flatten) .SelectMany(this.Flatten)
.Select(stack => stack.ToArray()); .Select(stack => stack.ToArray());
return anagramsWords.Select(list => string.Join(" ", list)); return anagramsWords.Select(list => string.Join(" ", list));
@ -53,7 +53,7 @@
T[] wordVariants; T[] wordVariants;
var newStack = phrase.Pop(out wordVariants); var newStack = phrase.Pop(out wordVariants);
return Flatten(newStack).SelectMany(remainder => wordVariants.Select(word => remainder.Push(word))); return this.Flatten(newStack).SelectMany(remainder => wordVariants.Select(word => remainder.Push(word)));
} }
} }
} }

@ -32,14 +32,13 @@
private long Iterations { get; set; } = 0; private long Iterations { get; set; } = 0;
// Produces all sequences of vectors with the target sum // Produces all sequences of vectors with the target sum
public IEnumerable<Vector<byte>[]> GenerateSequences(IEnumerable<Vector<byte>> vectors) public IEnumerable<Vector<byte>[]> GenerateSequences(IEnumerable<Vector<byte>> vectors)
{ {
var filteredVectors = FilterVectors(vectors); var filteredVectors = this.FilterVectors(vectors);
var dictionary = ImmutableStack.Create(filteredVectors.ToArray()); var dictionary = ImmutableStack.Create(filteredVectors.ToArray());
var unorderedSequences = GenerateUnorderedSequences(this.Target, ImmutableStack.Create<Vector<byte>>(), dictionary); var unorderedSequences = this.GenerateUnorderedSequences(this.Target, ImmutableStack.Create<Vector<byte>>(), dictionary);
var allSequences = unorderedSequences.SelectMany(GeneratePermutations); var allSequences = unorderedSequences.SelectMany(this.GeneratePermutations);
return allSequences; return allSequences;
} }
@ -75,7 +74,7 @@
Vector<byte> currentVector; Vector<byte> currentVector;
var nextDictionaryTail = dictionaryTail.Pop(out currentVector); var nextDictionaryTail = dictionaryTail.Pop(out currentVector);
DebugState(partialSumStack, currentVector); this.DebugState(partialSumStack, currentVector);
var newRemainder = remainder - currentVector; var newRemainder = remainder - currentVector;
if (newRemainder == Vector<byte>.Zero) if (newRemainder == Vector<byte>.Zero)
@ -84,7 +83,7 @@
} }
else if ((newRemainder & Negative) == Vector<byte>.Zero) else if ((newRemainder & Negative) == Vector<byte>.Zero)
{ {
foreach (var result in GenerateUnorderedSequences(newRemainder, partialSumStack.Push(currentVector), dictionaryTail)) foreach (var result in this.GenerateUnorderedSequences(newRemainder, partialSumStack.Push(currentVector), dictionaryTail))
{ {
yield return result; yield return result;
} }
@ -101,7 +100,7 @@
Vector<byte> currentVector; Vector<byte> currentVector;
dictionaryTail = dictionaryTail.Pop(out currentVector); dictionaryTail = dictionaryTail.Pop(out currentVector);
DebugState(partialSumStack, currentVector); this.DebugState(partialSumStack, currentVector);
var newRemainder = remainder - currentVector; var newRemainder = remainder - currentVector;
if (newRemainder == Vector<byte>.Zero) if (newRemainder == Vector<byte>.Zero)

Loading…
Cancel
Save