Skip to content

Commit

Permalink
soved day 02 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
deckelmouck committed Dec 2, 2023
1 parent c48821f commit df41b9f
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions 2023/Day02/solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,58 @@ public void SolvePart1()
public void SolvePart2()
{
Console.WriteLine("part2");

solutionBase sb = new();
var input = sb.getInputLines(@"2023/Day02/input");
//var input = sb.getInputLines(@"2023/Day02/testpartone");

List<Game> games = new();

foreach (var item in input)
{
string gamenumber = item.Split(':')[0].Replace("Game ", "");
Console.WriteLine(gamenumber);

int gno = Convert.ToInt32(gamenumber);

string plays = item.Split(':')[1];

List<Cubes> playlist = new();

foreach (var set in plays.Split(';'))
{
int red = 0;
int green = 0;
int blue = 0;

foreach (var c in set.Split(','))
{
if(c.Contains("red"))
{
red = GetIntOutOfString(c);
}
else if(c.Contains("green"))
{
green = GetIntOutOfString(c);
}
else if(c.Contains("blue"))
{
blue = GetIntOutOfString(c);
}
}

Cubes cubesSet = new Cubes(red, green, blue);
playlist.Add(cubesSet);
}

Game game = new Game(gno, playlist);
games.Add(game);
}

Console.WriteLine($"there are {games.Count} games in input");

int sum = games.Sum(x => x.Power());
Console.WriteLine($"the sum of all powers in: {sum} ");
}

int GetIntOutOfString(string input)
Expand All @@ -117,6 +169,20 @@ internal Game (int g, List<Cubes> lc)
game = g;
listCubes = lc;
}

internal int Power()
{
int maxRed = 0;
int maxGreen = 0;
int maxBlue = 0;

maxRed = listCubes.Max(x => x.red);
maxGreen = listCubes.Max(x => x.green);
maxBlue = listCubes.Max(x => x.blue);


return maxBlue * maxGreen * maxRed;
}
}

class Cubes
Expand Down

0 comments on commit df41b9f

Please sign in to comment.