Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
4.2 kB
4
Indexable
Never
using System;
using System.Collections.Generic;
using System.Linq;

namespace Day32022
{
    public class BackPack
    {
        public List<Compartment> Compartments { get; set; }
    }
    public class Compartment
    {
        public List<BackPackItem> BackPackItems { get; set; }
    }
    public class BackPackItem
    {
        public string Letter { get; set; }
        public int Value { get; set; }
    }
    class Program
    {
        public static List<string> Input => System.IO.File.ReadAllLines(@"input.txt").ToList();
        public static char[] UpperAlphabet  => "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
        public static char[] LowerAlphabet  => "abcdefghijklmnopqrstuvwxyz".ToCharArray();
        public static void Main(string[] args)
        {
            var backPackList = Input.Select(x => GetBackPack(x)).ToList();

            var valuesPart1 = new List<int>();
            var valuesPart2 = new List<int>();

            for (int i = 0; i < backPackList.Count / 3; i++)
            {
                var allBackPacks = new List<BackPack>();
                if (i > 0)
                {
                    allBackPacks = backPackList.Skip(3 * i).Take(3).ToList();
                }
                else
                {
                    allBackPacks = backPackList.Take(3).ToList();
                }
                var FirstBackPack = allBackPacks[0].Compartments.SelectMany(x => x.BackPackItems);
                var SecondBackPack = allBackPacks[1].Compartments.SelectMany(x => x.BackPackItems);
                var ThirdBackPack = allBackPacks[2].Compartments.SelectMany(x => x.BackPackItems);

                valuesPart2.Add(FirstBackPack.Where(x => SecondBackPack.FirstOrDefault(y => y.Value == x.Value) != null &&
                ThirdBackPack.FirstOrDefault(y => y.Value == x.Value) != null).FirstOrDefault().Value);
            }

            foreach (var backpack in backPackList)
            {
                var firstCompartment = backpack.Compartments.First().BackPackItems;
                var secondCompartment = backpack.Compartments.Last().BackPackItems;

                valuesPart1.Add(firstCompartment.Where(x => secondCompartment.FirstOrDefault(y => y.Value == x.Value) != null).FirstOrDefault().Value);
            }
            Console.WriteLine(valuesPart1.Sum());
            Console.WriteLine(valuesPart2.Sum());
            Console.ReadLine();
        }
        public static BackPack GetBackPack(string backpack)
        {
            return new BackPack { Compartments = GetCompartments(backpack) };
        }
        public static List<Compartment> GetCompartments(string backPack)
        {
            var half = backPack.Length / 2;
            var firstCompartmen = backPack.Take(half).ToArray();
            var secondCompartmen = backPack.Skip(half).Take(half).ToArray();

            return new List<Compartment>() 
            { 
                new Compartment() { BackPackItems = GetBackPackItems(firstCompartmen) },
                new Compartment() { BackPackItems = GetBackPackItems(secondCompartmen) },
            };
        }
        public static List<BackPackItem> GetBackPackItems(char[] charArray)
        {
            var backPackItemList = new List<BackPackItem>();
            for (int i = 0; i < charArray.Length; i++)
            {
               backPackItemList.Add(new BackPackItem() { Letter = charArray[i].ToString(), Value = GetLetterValue(charArray[i]) });
            }
            return backPackItemList;
        }
        public static int GetLetterValue(char c)
        {
            var value = 0;
            if (char.IsLower(c))
            {
                for (int i = 0; i < LowerAlphabet.Length; i++)
                {
                    if (c == LowerAlphabet[i])
                        value = i + 1;
                }
            }
            else
            {
                for (int i = 0; i < UpperAlphabet.Length; i++)
                {
                    if (c == UpperAlphabet[i])
                        value = i + 27;
                }
            }
            return value;
        }
    }
}