-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1.cs
66 lines (60 loc) · 1.91 KB
/
Day1.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
namespace advent_of_code_2022;
public class Day1
{
public static void SolveFirstPart(string[] args)
{
string[] lines = File.ReadAllLines("day1-input.txt");
int current = 0;
int currentMax = 0;
for (var i = 0; i < lines.Length; i++)
{
var line = lines[i];
if (string.IsNullOrEmpty(line))
{
currentMax = Math.Max(currentMax, current);
current = 0;
continue;
}
current += int.Parse(line);
}
currentMax = Math.Max(currentMax, current);
Console.WriteLine(currentMax);
}
public static void Solve(string[] args)
{
string[] lines = File.ReadAllLines("day1-input.txt");
int current = 0;
int firstMax = 0;
int secondMax = 0;
int thirdMax = 0;
for (var i = 0; i < lines.Length; i++)
{
var line = lines[i];
if (string.IsNullOrEmpty(line))
{
int currentMin;
firstMax = GetMaxAndOutMin(firstMax, current, out currentMin);
secondMax = GetMaxAndOutMin(secondMax, currentMin, out currentMin);
thirdMax = GetMaxAndOutMin(thirdMax, currentMin, out currentMin);
current = 0;
continue;
}
current += int.Parse(line);
}
int currentMinOut;
firstMax = GetMaxAndOutMin(firstMax, current, out currentMinOut);
secondMax = GetMaxAndOutMin(secondMax, currentMinOut, out currentMinOut);
thirdMax = GetMaxAndOutMin(thirdMax, currentMinOut, out currentMinOut);
Console.WriteLine(firstMax + secondMax + thirdMax);
}
private static int GetMaxAndOutMin(int n1, int n2, out int min)
{
if (n1 > n2)
{
min = n2;
return n1;
}
min = n1;
return n2;
}
}