publicclassInventoryManagement{publicintGetCheckSum(IEnumerable<string>inventory){returnGetCheckSum(GetCheckSumCandidates(inventory));}publicintGetCheckSum(IEnumerable<InventoryChecksumCandidates>candidates){varchecksumPieces=candidates.GroupBy(gb=>gb.DuplicateCount).Select(s=>new{s.Key,Count=s.Count()}).ToList();Debug.Assert(checksumPieces.Count()==2);returnchecksumPieces[0].Count*checksumPieces[1].Count;}publicIEnumerable<InventoryChecksumCandidates>GetCheckSumCandidates(IEnumerable<string>inventory){List<InventoryChecksumCandidates>list=newList<InventoryChecksumCandidates>();foreach(varitemininventory){varchecksumCandidate=item// make everything same case (just in case).ToLower()// where it's a letter.Where(char.IsLetter)// Group by the default (letter).GroupBy(gb=>gb)// Project the found values into their new type.Select(s=>newInventoryChecksumCandidates(){DuplicateCharacter=s.Key.ToString(),DuplicateCount=s.Count()});if(checksumCandidate!=null){list.AddIfNotNull(checksumCandidate.FirstOrDefault(f=>f.DuplicateCount==2));list.AddIfNotNull(checksumCandidate.FirstOrDefault(f=>f.DuplicateCount==3));}}returnlist;}}
publicclassPrototypeFabricFinder{publicstringGetProtoTypeFabric(IEnumerable<string>inventory){varinventoryPermutations=SwapCharForEachInventoryPermutation(inventory);varfoundDuplicateish=inventoryPermutations// Group by default.GroupBy(gb=>gb)// We only want the instance where there's more than// one in the group by (the prototype fabric).Where(w=>w.Count()>1).Select(s=>new{s.Key}).FirstOrDefault();// Return the prototype fabric, minus the "different" single characterreturnfoundDuplicateish.Key.Replace("*","");}/// <summary>/// Builds and returns a <see cref="IEnumerable{string}"/>/// containing every permutation of iventory items, with/// one character (*) swapped in at a differing index./// /// I have no idea if that makes sense./// </summary>/// <param name="inventory">each inventory id.</param>/// <returns></returns>privateIEnumerable<string>SwapCharForEachInventoryPermutation(IEnumerable<string>inventory){List<string>list=newList<string>();foreach(varitemininventory){// Create new strings and add them to the list.// The new strings will be a copy of the original,// with a single character (the current index) swapped in with "*"for(varindex=0;index<item.Length;index++){varcharArrayOfItem=item.ToCharArray();charArrayOfItem[index]='*';list.Add(newstring(charArrayOfItem));}}returnlist;}}
I didn't see any C# glancing through:
(Note
AddIfNotNullis from my extension methods package nuget.org/packages/Kritner.Extensi...Part 2:
I dunno how I'm gonna keep up during the week, but putting my solutions in the repo github.com/Kritner/Kritner.AdventO...