-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.cs
93 lines (82 loc) · 2.12 KB
/
Day2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.Text.RegularExpressions;
namespace AdventOfCode2023
{
internal class Day2
{
static readonly Dictionary<string, int> maxCubes = new(){
{"red", 12},
{"green", 13},
{"blue", 14}
};
public static void RunPuzzle1()
{
string[] lines = File.ReadAllLines(@"Day2-Input.txt");
int sumIDs = 0;
foreach (string line in lines)
{
Match matches = Regex.Match(line, "^Game ([0-9]+): (.*)");
int gameID = int.Parse(matches.Groups[1].Value);
string gamesLine = matches.Groups[2].Value;
bool gamePossible = true;
string[] sets = gamesLine.Split(';');
foreach (string set in sets)
{
string[] cubeSets = set.Split(',');
foreach (string cubeSet in cubeSets)
{
string cubeSetTrimmed = cubeSet.Trim();
string[] cube = cubeSetTrimmed.Split(' ');
int cubeAmount = int.Parse(cube[0]);
if (cubeAmount > maxCubes[cube[1]])
{
gamePossible = false;
}
}
}
if (gamePossible)
{
sumIDs += gameID;
}
}
Console.WriteLine("Answer Puzzle 1: " + sumIDs);
}
public static void RunPuzzle2()
{
string[] lines = File.ReadAllLines(@"Day2-Input.txt");
int sumPowers = 0;
foreach (string line in lines)
{
Match matches = Regex.Match(line, "^Game ([0-9]+): (.*)");
string gamesLine = matches.Groups[2].Value;
int maxRed = 0;
int maxBlue = 0;
int maxGreen = 0;
string[] sets = gamesLine.Split(';');
foreach (string set in sets)
{
string[] cubeSets = set.Split(',');
foreach (string cubeSet in cubeSets)
{
string cubeSetTrimmed = cubeSet.Trim();
string[] cube = cubeSetTrimmed.Split(' ');
int cubeAmount = int.Parse(cube[0]);
if (cube[1] == "red" && cubeAmount > maxRed)
{
maxRed = cubeAmount;
}
else if (cube[1] == "blue" && cubeAmount > maxBlue)
{
maxBlue = cubeAmount;
}
else if (cube[1] == "green" && cubeAmount > maxGreen)
{
maxGreen = cubeAmount;
}
}
}
sumPowers += (maxRed * maxBlue * maxGreen);
}
Console.WriteLine("Answer Puzzle 2: " + sumPowers);
}
}
}