AdventOfCode/Year2022/Day6.cs

27 lines
815 B
C#
Raw Normal View History

2023-06-07 22:58:57 +00:00
namespace AdventOfCode.Year2022;
public class Day6
{
2023-06-07 23:12:01 +00:00
private static readonly string Input = File.ReadAllText("inputs/day6.txt");
2023-06-07 22:58:57 +00:00
public Day6()
{
Console.WriteLine("Day6 Solution");
Console.WriteLine($"Part1 Result: {GetValidMarkerIndex(4)}");
Console.WriteLine($"Part2 Result: {GetValidMarkerIndex(14)}");
Console.WriteLine("\n=============================\n");
}
private static int? GetValidMarkerIndex(int size)
{
2023-06-07 23:12:01 +00:00
for (int i = 0; i < Input.Length-size; i++)
if (ValidateMarker(Input.Substring(i, size)))
2023-06-07 22:58:57 +00:00
return i + size;
return null;
}
private static bool ValidateMarker(string marker)
{
return !marker.Where((c1, i) => marker.Where((c2, j) => c1 == c2 && i != j).Any()).Any();
}
}