-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.cs
90 lines (85 loc) · 2.33 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
//Part 1
string[] input = File.ReadAllLines("input.txt");
Dictionary<string, int> colourLimit = new Dictionary<string, int> { { "red", 12 }, { "green", 13 }, { "blue", 14 } };
int sum = 0;
foreach (string s in input)
{
string[] content = s.Split(':');
string[] gameSets = content[1].Split(';');
bool possible = true;
foreach (string colour in gameSets.Select(set => set.Split(',')).SelectMany(subset => subset))
{
if (colour.Contains("blue"))
{
int blue = int.Parse(colour.Trim().Split(' ')[0]);
if (blue > colourLimit["blue"])
{
possible = false;
break;
}
}
if (colour.Contains("red"))
{
int red = int.Parse(colour.Trim().Split(' ')[0]);
if (red > colourLimit["red"])
{
possible = false;
break;
}
}
if (colour.Contains("green"))
{
int green = int.Parse(colour.Trim().Split(' ')[0]);
if (green > colourLimit["green"])
{
possible = false;
break;
}
}
}
if (possible)
{
sum += int.Parse(content[0].Split(' ')[1]);
}
}
Console.WriteLine(sum);
//Part 2
string[] input = File.ReadAllLines("input.txt");
int sum = 0;
foreach (string s in input)
{
string[] content = s.Split(':');
string[] gameSets = content[1].Split(';');
int smallBlue = 1;
int smallRed = 1;
int smallGreen = 1;
foreach (string colour in gameSets.Select(set => set.Split(',')).SelectMany(subset => subset))
{
if (colour.Contains("blue"))
{
int blue = int.Parse(colour.Trim().Split(' ')[0]);
if (blue > smallBlue)
{
smallBlue = blue;
}
}
if (colour.Contains("red"))
{
int red = int.Parse(colour.Trim().Split(' ')[0]);
if (red > smallRed)
{
smallRed = red;
}
}
if (colour.Contains("green"))
{
int green = int.Parse(colour.Trim().Split(' ')[0]);
if (green > smallGreen)
{
smallGreen = green;
}
}
}
sum += smallGreen * smallBlue * smallRed;
}
Console.WriteLine(sum);