Last active
December 6, 2022 16:56
-
-
Save decriptor/f1427e7e8cc57dd212a56445bc1ff1f4 to your computer and use it in GitHub Desktop.
Day6_2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var file = @"c:\code\AoC\AoC2022\Day6\input.txt"; | |
var message = File.ReadAllText(file).AsSpan(); | |
var set = new HashSet<char>(4); | |
var part1 = FindUniqueSegment(message, 4); | |
var part2 = FindUniqueSegment(message, 14); | |
(part1 == 1760).Dump("Part 1"); | |
(part2 == 2974).Dump("Part 2"); | |
int FindUniqueSegment(ReadOnlySpan<char> message, int segmentLength) | |
{ | |
for (int i = 0; i < message.Length - segmentLength - 1; i++) | |
{ | |
if (CheckForDuplicates(message.Slice(i, segmentLength), segmentLength)) | |
{ | |
return (i + segmentLength); | |
} | |
} | |
return -1; | |
} | |
bool CheckForDuplicates(ReadOnlySpan<char> chars, int count) | |
{ | |
set.Clear(); | |
foreach(var c in chars) | |
set.Add(c); | |
return set.Count == count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment