Advertisement
EdgeLordKirito

AdventOfCodeDay2

Dec 2nd, 2023
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.60 KB | Software | 0 0
  1. internal partial class _2023Day2
  2. {
  3.     private List<ElfGame> Games { get; set; }
  4.  
  5.     public int RedConstraint { get; private set; }
  6.  
  7.     public int GreenConstraint { get; private set; }
  8.  
  9.     public int BlueConstraint { get; private set; }
  10.  
  11.     public _2023Day2(string[] input,int Red, int Green, int Blue)
  12.     {
  13.         Games = new List<ElfGame>();
  14.         foreach (string line in input)
  15.         {
  16.             Games.Add(new ElfGame(line));
  17.         }
  18.         RedConstraint = Red;
  19.         GreenConstraint = Green;
  20.         BlueConstraint = Blue;
  21.     }
  22.  
  23.     public int DoPart1()
  24.     {
  25.         int result = 0;
  26.         foreach (ElfGame game in Games)
  27.         {
  28.             if (IsGamePossibleUnderConstraints(game))
  29.             {
  30.                 result += game.Id;
  31.             }
  32.         }
  33.         return result;
  34.     }
  35.  
  36.     public int DoPart2()
  37.     {
  38.         int result = 0;
  39.         foreach (ElfGame game in Games)
  40.         {
  41.             result += game.CalculateGamePower();
  42.         }
  43.         return result;
  44.     }
  45.  
  46.     private bool IsGamePossibleUnderConstraints(ElfGame game)
  47.     {
  48.         return game.IsPossibleUnderConstraints(RedConstraint,GreenConstraint,BlueConstraint);
  49.     }
  50.  
  51.     private partial class ElfGame
  52.     {
  53.         public int Id { get; private set; }
  54.         private List<Pull> Pulls { get; set; }
  55.  
  56.         public ElfGame(string input)
  57.         {
  58.             string[] split = Split(input);
  59.             Id = ExtractId(split[0]);
  60.             Pulls = CreatePulls(split[1]);
  61.         }
  62.  
  63.         private string[] Split(string input)
  64.         {
  65.             input = input.Trim();
  66.             return input.Split(':');
  67.         }
  68.  
  69.         private int ExtractId(string input)
  70.         {
  71.             string id = input.Split(' ')[1];
  72.             return int.Parse(id);
  73.         }
  74.  
  75.         private string[] SplitPulls(string input)
  76.         {
  77.             string[] split = input.Split(';');
  78.             return split;
  79.         }
  80.  
  81.         private List<Pull> CreatePulls(string input)
  82.         {
  83.             string[] split = SplitPulls(input);
  84.             List<Pull> result = new List<Pull>();
  85.             foreach (string pull in split)
  86.             {
  87.                 result.Add(CreatePull(pull));
  88.             }
  89.  
  90.             return result;
  91.         }
  92.  
  93.         [GeneratedRegex(@"\b\d+\s*(red)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
  94.         private static partial Regex ExtractRed();
  95.         [GeneratedRegex(@"\b\d+\s*(green)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
  96.         private static partial Regex ExtractGreen();
  97.         [GeneratedRegex(@"\b\d+\s*(blue)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
  98.         private static partial Regex ExtractBlue();
  99.  
  100.         private Pull CreatePull(string input)
  101.         {
  102.             var red = ExtractRed().Matches(input);
  103.             var green = ExtractGreen().Matches(input);
  104.             var blue = ExtractBlue().Matches(input);
  105.             Pull result = new Pull(ExtractValue(red), ExtractValue(green), ExtractValue(blue));
  106.             return result;
  107.         }
  108.  
  109.         private int ExtractValue(MatchCollection matches)
  110.         {
  111.             if (matches.Count == 0)
  112.             {
  113.                 return 0;
  114.             }
  115.  
  116.             string value = matches.First().Value;
  117.             string[] splits = value.Split(' ');
  118.             return int.Parse(splits[0]);
  119.         }
  120.  
  121.         public bool IsPossibleUnderConstraints(int red, int green, int blue)
  122.         {
  123.             foreach (Pull pull in Pulls)
  124.             {
  125.                 if (pull.Red > red || pull.Green > green || pull.Blue > blue)
  126.                 {
  127.                     return false;
  128.                 }
  129.             }
  130.  
  131.             return true;
  132.         }
  133.  
  134.         private (int red, int green, int blue) GetMaximumValues()
  135.         {
  136.             int red = MaximumRed();
  137.             int green = MaximumGreen();
  138.             int blue = MaximumBlue();
  139.  
  140.             return (red, green, blue);
  141.         }
  142.  
  143.         private int MaximumRed()
  144.         {
  145.             int result = int.MinValue;
  146.             foreach (Pull pull in Pulls)
  147.             {
  148.                 if (result < pull.Red)
  149.                 {
  150.                     result = pull.Red;
  151.                 }
  152.             }
  153.             return result;
  154.         }
  155.  
  156.         private int MaximumGreen()
  157.         {
  158.             int result = int.MinValue;
  159.             foreach (Pull pull in Pulls)
  160.             {
  161.                 if (result < pull.Green)
  162.                 {
  163.                     result = pull.Green;
  164.                 }
  165.             }
  166.             return result;
  167.         }
  168.  
  169.         private int MaximumBlue()
  170.         {
  171.             int result = int.MinValue;
  172.             foreach (Pull pull in Pulls)
  173.             {
  174.                 if (result < pull.Blue)
  175.                 {
  176.                     result = pull.Blue;
  177.                 }
  178.             }
  179.             return result;
  180.         }
  181.  
  182.         private int Power((int red,int green,int blue) minimal)
  183.         {
  184.             int result = minimal.red * minimal.green * minimal.blue;
  185.             return result;
  186.         }
  187.  
  188.         public int CalculateGamePower()
  189.         {
  190.             var minimal = GetMaximumValues();
  191.             return Power(minimal);
  192.         }
  193.  
  194.         private class Pull
  195.         {
  196.             public Pull(int red, int green, int blue)
  197.             {
  198.                 Red = red;
  199.                 Green = green;
  200.                 Blue = blue;
  201.             }
  202.  
  203.             public int Red { get; private set; }
  204.             public int Green { get; private set; }
  205.             public int Blue { get; private set; }
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement