You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TrustPilotChallenge/WhiteRabbit/Program.cs

101 lines
3.6 KiB

namespace WhiteRabbit
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
8 years ago
using System.Numerics;
using System.Text;
using Org.BouncyCastle.Crypto.Digests;
/// <summary>
/// Main class
/// </summary>
public static class Program
{
private const string SourcePhrase = "poultry outwits ants";
private const int MaxWordsInPhrase = 4;
/// <summary>
/// Main entry point
/// </summary>
public static void Main()
{
var stopwatch = new Stopwatch();
stopwatch.Start();
8 years ago
var expectedHashes = new[]
{
8 years ago
"e4820b45d2277f3844eac66c903e84be",
"23170acc097c24edb98fc5488ab033fe",
"665e5bcb0c20062fe8abaaf4628bb154",
};
8 years ago
var expectedHashesAsVectors = expectedHashes.Select(hash => new Vector<byte>(HexadecimalStringToByteArray(hash))).ToArray();
8 years ago
var processor = new StringsProcessor(Encoding.ASCII.GetBytes(SourcePhrase), MaxWordsInPhrase, ReadInput());
Console.WriteLine($"Initialization complete; time from start: {stopwatch.Elapsed}");
#if DEBUG
// it makes the program slow (as all anagrams are generated twice), but this code is only run in a debug mode
var totalAnagramsCount = processor.GeneratePhrases().Count();
Console.WriteLine($"Total anagrams count: {totalAnagramsCount}; time from start: {stopwatch.Elapsed}");
#endif
processor.GeneratePhrases()
8 years ago
.Select(phraseBytes => new { phraseBytes, hashVector = ComputeHashVector(phraseBytes) })
.Where(tuple => Array.IndexOf(expectedHashesAsVectors, tuple.hashVector) >= 0)
.Select(tuple => new { phrase = Encoding.ASCII.GetString(tuple.phraseBytes), hash = VectorToHexadecimalString(tuple.hashVector) })
.ForAll(phraseInfo => Console.WriteLine($"Found phrase for {phraseInfo.hash}: {phraseInfo.phrase}; time from start is {stopwatch.Elapsed}"));
stopwatch.Stop();
Console.WriteLine($"Done; time from start: {stopwatch.Elapsed}");
}
8 years ago
// Code taken from http://stackoverflow.com/a/321404/831314
8 years ago
private static byte[] HexadecimalStringToByteArray(string hex)
8 years ago
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
// Bouncy Castle is used instead of standard .NET methods for performance reasons
8 years ago
private static Vector<byte> ComputeHashVector(byte[] input)
{
var digest = new MD5Digest();
digest.BlockUpdate(input, 0, input.Length);
byte[] hash = new byte[16];
digest.DoFinal(hash, 0);
return new Vector<byte>(hash);
}
8 years ago
private static string VectorToHexadecimalString(Vector<byte> hash)
{
return string.Concat(Enumerable.Range(0, 16).Select(i => hash[i].ToString("x2")));
}
private static IEnumerable<byte[]> ReadInput()
{
string line;
while ((line = Console.ReadLine()) != null)
{
yield return Encoding.ASCII.GetBytes(line);
}
}
#if SINGLE_THREADED
private static void ForAll<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var entry in source)
{
action(entry);
}
}
#endif
}
}