A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may asked for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.
The text file, keylog.txt, contains fifty successful login attempts.
Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.
Did this by hand but then I felt like I had to come up with a coded solution too.
using System;
using System.Collections.Generic;
using System.IO;
namespace ProjectEuler
{
class Program
{
static void Main()
{
//Problem 79
DateTime start = DateTime.Now;
var sr = new StreamReader(@"../../keylog.txt");
var keys = new List<string>();
while (!sr.EndOfStream)keys.Add(sr.ReadLine());
for (int i = 0; i < keys.Count - 1; i++)
for (int j = i + 1; j < keys.Count; j++)
if (keys[i] == keys[j])
keys.Remove(keys[j]);
var cList = new List<char>();
foreach (string s in keys)
{
char[] c = s.ToCharArray();
foreach (char c1 in c)
if (!cList.Contains(c1))
cList.Add(c1);
}
var answer=new char[cList.Count];
foreach (char c in cList)
{
var digitsRight = new List<char>();
foreach (string s in keys)
if (s.Contains(c.ToString()))
{
int n = s.IndexOf(c);
if (n != s.Length - 1)
for (int i = n + 1; i < s.Length; i++)
{
char d = char.Parse(s.Substring(i, 1));
if (!digitsRight.Contains(d))
digitsRight.Add(d);
}
}
answer[cList.Count-(digitsRight.Count+1)] = c;
}
Console.WriteLine(new string(answer));
TimeSpan time = DateTime.Now - start;
Console.WriteLine("This took {0}", time);
Console.ReadKey();
}
}
}