Day Two in CSharp

main
Jaime9070 2023-12-03 17:56:32 +01:00
parent cf88d6ab13
commit 2b80257de8
5 changed files with 107 additions and 0 deletions

View File

@ -9,12 +9,16 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\DayOne\DayOne.csproj" /> <ProjectReference Include="..\DayOne\DayOne.csproj" />
<ProjectReference Include="..\DayTwo\DayTwo.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="RandomFiles\calibraciones.txt"> <None Update="RandomFiles\calibraciones.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Update="RandomFiles\cubes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,4 +1,5 @@
using DayOne; using DayOne;
using DayTwo;
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
@ -7,4 +8,8 @@ Console.WriteLine("Hello, World!");
//Console.WriteLine($"The calibration found was {calibrationFound}"); //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(); Console.ReadLine();

View File

@ -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

View File

@ -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;
}
}

View File

@ -0,0 +1,67 @@
namespace DayTwo;
public class Game
{
public int Id { get; set; }
public ICollection<string> 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<string, int>();
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;
}
}