Managed to remember to start on time. Also, I managed to convince a friend I go rock climbing with to give Advent of Code a try. Hi Ashy!
This one seems kinda silly. Just take each number as a string, split it in half, and check if the halves match. Easy!
private static bool IsValidId(long id)
{
string idString = id.ToString();
// Odd-length strings should always be valid
if (idString.Length % 2 == 1) return true;
int halfLength = idString.Length / 2;
return !idString[..halfLength].Equals(idString[halfLength..]);
}
Hmm. For part 2, just an extension. Maybe just abstract the check a bit, and be sure to check all iterations up to the half size?
private static bool IsValidIdOfSize(long id, int size)
{
string idString = id.ToString();
// Odd-length strings should always be valid
if (idString.Length % size != 0) return true;
string pattern = idString[..size];
for (int i = size; i < idString.Length; i += size)
{
if (!idString.Substring(i, size).Equals(pattern)) return true;
}
return false;
}