re: AoC Day 2: Inventory Management System VIEW POST

FULL DISCUSSION
 

I didn't see any C# glancing through:

public class InventoryManagement
    {
        public int GetCheckSum(IEnumerable<string> inventory)
        {
            return GetCheckSum(GetCheckSumCandidates(inventory));
        }

        public int GetCheckSum(IEnumerable<InventoryChecksumCandidates> candidates)
        {
            var checksumPieces = candidates
                .GroupBy(gb => gb.DuplicateCount)
                .Select(s => new
                {
                    s.Key,
                    Count = s.Count()
                })
                .ToList();

            Debug.Assert(checksumPieces.Count() == 2);

            return checksumPieces[0].Count * checksumPieces[1].Count;
        }

        public IEnumerable<InventoryChecksumCandidates> GetCheckSumCandidates(IEnumerable<string> inventory)
        {
            List<InventoryChecksumCandidates> list = new List<InventoryChecksumCandidates>();

            foreach (var item in inventory)
            {
                var checksumCandidate = 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 => new InventoryChecksumCandidates()
                    {
                        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)
                    );
                }
            }

            return list;
        }
    }

(Note AddIfNotNull is from my extension methods package nuget.org/packages/Kritner.Extensi...

Part 2:

public class PrototypeFabricFinder
    {
        public string GetProtoTypeFabric(IEnumerable<string> inventory)
        {
            var inventoryPermutations = SwapCharForEachInventoryPermutation(inventory);
            var foundDuplicateish = 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 character
            return foundDuplicateish.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>
        private IEnumerable<string> SwapCharForEachInventoryPermutation(
            IEnumerable<string> inventory
        )
        {
            List<string> list = new List<string>();

            foreach (var item in inventory)
            {
                // 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 (var index = 0; index < item.Length; index++)
                {
                    var charArrayOfItem = item.ToCharArray();
                    charArrayOfItem[index] = '*';

                    list.Add(new string(charArrayOfItem));
                }
            }

            return list;
        }
    }

I dunno how I'm gonna keep up during the week, but putting my solutions in the repo github.com/Kritner/Kritner.AdventO...

code of conduct - report abuse