Day 5 would have been more interesting (but more time consuming) if there had been 12 (or anything more than about 8 or 9) numbers in the password rather than 6.
I was thinking along the lines of:
function count(digits)
return count(digits : digits.last()) + count(digits : digits.last() + 1) + ... + count(digits : 9)
then shortcutting in some way if optimization was necessary.
As it is the obvious way is to run a check on each possible number as there are only of the order of 10^6.
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Day04
{
class Program
{
static int inputStart = 265275; //my inputs
static int inputEnd = 781584;
static void Main()
{
var candidates = Enumerable.Range(inputStart, inputEnd + 1 - inputStart).ToArray();
var c = candidates.Where(i => Matches(i));
Console.WriteLine(c.Count());
}
static bool Matches(int x)
{
var s = x.ToString();
for (int i = 1; i < s.Length; i++)
if (s[i] < s[i - 1]) return false;
s = Regex.Replace(s, "1{3,}|2{3,}|3{3,}|4{3,}|5{3,}|6{3,}|7{3,}|8{3,}|9{3,}", "");
return Regex.IsMatch(s, "11|22|33|44|55|66|77|88|99");
}
}
}