2022-12-03 05:55:49 +00:00
|
|
|
namespace AOC2015;
|
2022-11-11 18:42:42 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-12-03 05:41:38 +00:00
|
|
|
/// Day 11: <a href="https://adventofcode.com/2015/day/11"/>
|
2022-11-11 18:42:42 +00:00
|
|
|
/// </summary>
|
2023-09-20 18:38:58 +00:00
|
|
|
public sealed class Day11() : Day(2015, 11, "Corporate Policy")
|
2022-11-11 18:42:42 +00:00
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
private char[]? _password;
|
2022-12-04 18:15:35 +00:00
|
|
|
|
2023-12-01 07:30:47 +00:00
|
|
|
public override void ProcessInput() => _password = Input.First().ToCharArray();
|
2022-12-04 18:15:35 +00:00
|
|
|
|
|
|
|
public override object Part1()
|
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
while (!IsValid(ref _password!)) Increment(ref _password);
|
2022-12-04 18:15:35 +00:00
|
|
|
|
|
|
|
return new string(_password);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object Part2()
|
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
Increment(ref _password!);
|
2022-12-04 18:15:35 +00:00
|
|
|
while (!IsValid(ref _password)) Increment(ref _password);
|
|
|
|
|
|
|
|
return new string(_password);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsValid(ref char[] password)
|
2022-11-11 18:42:42 +00:00
|
|
|
{
|
2022-12-04 18:15:35 +00:00
|
|
|
bool check1 = false, check3 = false;
|
|
|
|
for (var j = 2; j < password.Length; j++)
|
|
|
|
{
|
|
|
|
if (password[j] == 'i' || password[j] == 'o' || password[j] == 'l')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (password[j - 2] + 1 == password[j - 1] && password[j - 1] + 1 == password[j])
|
|
|
|
check1 = true;
|
|
|
|
|
|
|
|
if (j <= 2) continue;
|
|
|
|
|
|
|
|
for (var k = 0; k + 2 < j; k++)
|
|
|
|
if (password[j - 3 - k] == password[j - 2 - k]
|
|
|
|
&& password[j - 1] == password[j]
|
|
|
|
&& password[j - 2 - k] != password[j - 1])
|
|
|
|
{
|
|
|
|
check3 = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return check1 && check3;
|
2022-11-11 18:42:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:15:35 +00:00
|
|
|
private static void Increment(ref char[] password, int at = -1)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (at == -1) at = password.Length - 1;
|
2022-11-11 18:42:42 +00:00
|
|
|
|
2022-12-04 18:15:35 +00:00
|
|
|
password[at]++;
|
|
|
|
if (password[at] == 'i' || password[at] == 'o' || password[at] == 'l') password[at]++;
|
|
|
|
if (password[at] <= 'z') return;
|
|
|
|
password[at] = 'a';
|
|
|
|
at--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|