Created
December 3, 2022 07:43
-
-
Save decriptor/e57103dc36ef43f613958c636cf1fdbc to your computer and use it in GitHub Desktop.
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 = @"d:\code\decriptor\input3.txt"; | |
var input = File.ReadLines(file).ToArray(); | |
var priority = 0; | |
var badgePriority = 0; | |
for(int i = 0; i < input.Count(); i += 3) | |
{ | |
var input1 = input[i]; | |
var input2 = input[i+1]; | |
var input3 = input[i+2]; | |
priority += GetCharValue(FindCommonItem(SplitString(input1))); | |
priority += GetCharValue(FindCommonItem(SplitString(input2))); | |
priority += GetCharValue(FindCommonItem(SplitString(input3))); | |
var badge = input1 | |
.Intersect(input2) | |
.Intersect(input3) | |
.First(); | |
badgePriority += GetCharValue(badge); | |
} | |
IEnumerable<char>[] SplitString(string input) | |
{ | |
var half = input.Length / 2; | |
var first = input.Take(half); | |
var second = input.TakeLast(half); | |
return new[] { first, second }; | |
} | |
char FindCommonItem (IEnumerable<char>[] parts) | |
{ | |
return parts[0].Intersect(parts[1]).First(); | |
} | |
int GetCharValue(char item) | |
{ | |
if (Char.IsLower(item)) | |
return (int)item - 96; | |
return(int)item - 38; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment