AdventOfCode/Year2022/Day6.cs

29 lines
721 B
C#
Raw Permalink 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-24 21:26:39 +00:00
2023-06-07 22:58:57 +00:00
public Day6()
{
2023-06-24 21:26:39 +00:00
Console.WriteLine($@"
Day6 Solution
Part1 Result: {GetValidMarkerIndex(4)}
Part2 Result: {GetValidMarkerIndex(14)}
=============================");
2023-06-07 22:58:57 +00:00
}
private static int? GetValidMarkerIndex(int size)
{
2023-06-24 21:26:39 +00:00
for (var i = 0; i < Input.Length - size; i++)
2023-06-07 23:12:01 +00:00
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();
}
}