Skip to content

Instantly share code, notes, and snippets.

@nk0t
Created December 25, 2012 16:48
Show Gist options
  • Save nk0t/4374149 to your computer and use it in GitHub Desktop.
Save nk0t/4374149 to your computer and use it in GitHub Desktop.
総当りプログラム
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bruteforce
{
class Program
{
const string letters = "0123456789abcdef";
static void Main(string[] args)
{
foreach (var str in Bruteforce(letters, 4))
{
Console.WriteLine(str);
}
}
public static IEnumerable<string> Bruteforce(string letters,int len)
{
var num = new List<int>(new[] { -1 });
while (true)
{
int p = 0;
while (true)
{
if (p == num.Count)
num.Add(0);
if (num.Count > len)
yield break;
num[p]++;
if (num[p] >= letters.Length)
{
num[p] = 0;
p++;
continue;
}
break;
}
yield return new string(num.Select(s => letters[s]).Reverse().ToArray());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment