Skip to content

Instantly share code, notes, and snippets.

@johnboker
Created December 11, 2015 16:27
Show Gist options
  • Save johnboker/b330c2d6dcd6db15094f to your computer and use it in GitHub Desktop.
Save johnboker/b330c2d6dcd6db15094f to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCodeDay11
{
class Program
{
static void Main(string[] args)
{
var password = "hxbxwxba".Reverse().ToArray();
var cnt = 0;
var start = DateTime.Now;
var foundCount = 0;
do
{
for (var i = 0; i < password.Length; i++)
{
bool carry;
if (password[i] == 'z')
{
password[i] = 'a';
carry = true;
}
else
{
password[i]++;
carry = false;
}
if (password[i] == 'i' || password[i] == 'o' || password[i] == 'l') password[i]++;
if (!carry) break;
}
cnt++;
if (cnt % 1000000 == 0)
{
var p = new string(password.Reverse().ToArray());
var sec = (DateTime.Now - start).TotalSeconds;
var ps = Math.Round(cnt / sec);
Console.WriteLine($"{cnt} : {p} : {ps}/sec");
}
if (TestPassword(password.Reverse().ToArray()))
{
foundCount++;
Console.WriteLine($"{foundCount} : {cnt}: {new string(password.Reverse().ToArray())}");
}
} while (foundCount <= 5);
Console.WriteLine($"{cnt}: {new string(password.Reverse().ToArray())}");
Console.WriteLine("Done");
Console.ReadLine();
}
private static bool TestPassword(char[] password)
{
return ContainsThreeLetterRun(password) && ContainsDoublePair(password);
}
public static bool ContainsThreeLetterRun(char[] s)
{
for (var i = 0; i < s.Length - 2; i++)
{
if ((s[i] + 1) == s[i + 1] && (s[i] + 2) == s[i + 2])
{
return true;
}
}
return false;
}
public static bool ContainsDoublePair(char[] s)
{
var pairs = new List<Tuple<string, int>>();
for (var i = 0; i < s.Length - 1; i++)
{
var pair = $"{s[i]}{s[i + 1]}";
if (pair[0] == pair[1] && pairs.All(a => a.Item1 != pair))
{
pairs.Add(new Tuple<string, int>(pair, i));
}
}
return pairs.Count > 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment