Day 6 solution

This commit is contained in:
2022-12-05 23:28:34 -06:00
parent b9161e4b42
commit a3b906f94d
4 changed files with 47 additions and 1 deletions

44
src/06.cs Normal file
View File

@ -0,0 +1,44 @@
namespace aoc2022;
internal class Day06 : Day
{
private string? buffer;
internal override void Parse()
{
buffer = Util.ReadAllText("06");
}
private static int FindDistinct(string buf, int distinctLen)
{
Queue<char> s = new(distinctLen);
for (int i = 0; i < buf.Length; i++)
{
var c = buf[i];
s.Enqueue(c);
if (s.Count < distinctLen)
{
continue;
}
if (s.Distinct().Count() == distinctLen)
{
return i + 1;
}
s.Dequeue();
}
throw new Exception("didn't find anything");
}
internal override string Part1()
{
return $"First span of 4 distinct characters at character <+white>{FindDistinct(buffer!, 4)}";
}
internal override string Part2()
{
return $"First span of 14 distinct characters at character <+white>{FindDistinct(buffer!, 14)}";
}
}