Skip to content

Instantly share code, notes, and snippets.

@Trinitek
Created November 14, 2018 08:55
Show Gist options
  • Select an option

  • Save Trinitek/36a61babfcd4f7d1c7ca0e8333e06abd to your computer and use it in GitHub Desktop.

Select an option

Save Trinitek/36a61babfcd4f7d1c7ca0e8333e06abd to your computer and use it in GitHub Desktop.
[STAThread]
static void Main(string[] args)
{
while(true)
{
Console.WriteLine("ID?");
var id = Console.ReadLine();
Console.WriteLine(Decrement(id));
}
}
private static readonly char[] ValidChars = new char[]
{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
private static string Decrement(string id)
{
var idChars = id.ToCharArray().Reverse();
var newId = new List<char>();
bool wrapNext = true;
foreach (var c in idChars)
{
if (wrapNext)
{
var newC = Previous(c);
newId.Add(newC);
wrapNext = newC == ValidChars[ValidChars.Length - 1];
}
else
{
newId.Add(c);
}
}
newId.Reverse();
return new string(newId.ToArray());
}
private static char Previous(char c)
{
var i = Array.IndexOf(ValidChars, c) - 1;
return ValidChars[i < 0 ? ValidChars.Length - 1 : i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment