From 2b80257de8e1623a80e6ed6850b34c79dced0662 Mon Sep 17 00:00:00 2001 From: Jaime9070 Date: Sun, 3 Dec 2023 17:56:32 +0100 Subject: [PATCH] Day Two in CSharp --- .../AdventOfCode/AdventOfCode.csproj | 4 ++ CSharp/AdventOfCode/AdventOfCode/Program.cs | 5 ++ .../AdventOfCode/RandomFiles/cubes.txt | 5 ++ CSharp/AdventOfCode/DayTwo/CubeGameMaster.cs | 26 +++++++ CSharp/AdventOfCode/DayTwo/Game.cs | 67 +++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 CSharp/AdventOfCode/DayTwo/CubeGameMaster.cs create mode 100644 CSharp/AdventOfCode/DayTwo/Game.cs diff --git a/CSharp/AdventOfCode/AdventOfCode/AdventOfCode.csproj b/CSharp/AdventOfCode/AdventOfCode/AdventOfCode.csproj index 6f52aec..3e4d637 100644 --- a/CSharp/AdventOfCode/AdventOfCode/AdventOfCode.csproj +++ b/CSharp/AdventOfCode/AdventOfCode/AdventOfCode.csproj @@ -9,12 +9,16 @@ + Always + + Always + diff --git a/CSharp/AdventOfCode/AdventOfCode/Program.cs b/CSharp/AdventOfCode/AdventOfCode/Program.cs index 4a689a5..e855c14 100644 --- a/CSharp/AdventOfCode/AdventOfCode/Program.cs +++ b/CSharp/AdventOfCode/AdventOfCode/Program.cs @@ -1,4 +1,5 @@ using DayOne; +using DayTwo; Console.WriteLine("Hello, World!"); @@ -7,4 +8,8 @@ Console.WriteLine("Hello, World!"); //Console.WriteLine($"The calibration found was {calibrationFound}"); +var gameMaster = new CubeGameMaster(@"RandomFiles\cubes.txt"); + +var sumOfIds = gameMaster.ObtainSumOfValidGames(); +Console.WriteLine($"Sum of the Ids is {sumOfIds}"); Console.ReadLine(); \ No newline at end of file diff --git a/CSharp/AdventOfCode/AdventOfCode/RandomFiles/cubes.txt b/CSharp/AdventOfCode/AdventOfCode/RandomFiles/cubes.txt index e69de29..1cd7d33 100644 --- a/CSharp/AdventOfCode/AdventOfCode/RandomFiles/cubes.txt +++ b/CSharp/AdventOfCode/AdventOfCode/RandomFiles/cubes.txt @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green \ No newline at end of file diff --git a/CSharp/AdventOfCode/DayTwo/CubeGameMaster.cs b/CSharp/AdventOfCode/DayTwo/CubeGameMaster.cs new file mode 100644 index 0000000..8154537 --- /dev/null +++ b/CSharp/AdventOfCode/DayTwo/CubeGameMaster.cs @@ -0,0 +1,26 @@ +namespace DayTwo; + +public class CubeGameMaster +{ + public string FilePath { get; set; } + + public CubeGameMaster(string filePath) + { + FilePath = filePath; + } + + public int ObtainSumOfValidGames() + { + using StreamReader reader = new StreamReader(FilePath); + var sumOfValidGames = 0; + while (!reader.EndOfStream) + { + // Read a line from the file + string line = reader.ReadLine() ?? "Something failed"; + var game = new Game(line); + + sumOfValidGames += game.GetIdIfValid(); + } + return sumOfValidGames; + } +} \ No newline at end of file diff --git a/CSharp/AdventOfCode/DayTwo/Game.cs b/CSharp/AdventOfCode/DayTwo/Game.cs new file mode 100644 index 0000000..a00357b --- /dev/null +++ b/CSharp/AdventOfCode/DayTwo/Game.cs @@ -0,0 +1,67 @@ +namespace DayTwo; + +public class Game +{ + public int Id { get; set; } + public ICollection Steps { get; set; } + + private const int BLUE = 14; + private const int RED = 12; + private const int GREEN = 13; + + public Game(string line) + { + var headers = line.Split(':'); + + var id = headers[0].Split(' ')[1]; + + Id = int.Parse(id); + + Steps = headers[1].Split(';'); + } + + public int GetIdIfValid() + { + if (IsGameValid()) + { + return Id; + } + return 0; + } + + private bool IsGameValid() + { + foreach (var step in Steps) + { + var colorDictionary = new Dictionary(); + var colorValues = step.Split(","); + foreach (var colorNumber in colorValues) + { + var separatedValues = colorNumber.Split(" "); + colorDictionary.Add(separatedValues[2], int.Parse(separatedValues[1])); + } + foreach (var colors in colorDictionary) + { + switch (colors.Key) + { + case "red": + if (colors.Value > RED) return false; + break; + + case "green": + if (colors.Value > GREEN) return false; + break; + + case "blue": + if (colors.Value > BLUE) return false; + break; + + default: + break; + } + } + } + + return true; + } +} \ No newline at end of file