-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1_Pt2.linq
52 lines (40 loc) · 1.14 KB
/
Day1_Pt2.linq
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
void Main()
{
string[] input = File.ReadAllText(@"c:\Users\Kaylyn\Desktop\AOC\Day1_A.txt").Trim().Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
//string[] testInput = new string[] { "1969" };
double result = CalculateFuelRequirements(input);
Console.WriteLine(result);
}
double CalculateFuelRequirements(string[] input)
{
double result = 0;
foreach (string line in input)
{
double.TryParse(line, out double asNumber);
if (asNumber > 0)
{
double resultOfDiv = DivideByThree(asNumber);
double resultOfSubtraction = resultOfDiv - 2;
double resultPtTwo = CalculateFuelForFuel(resultOfSubtraction);
result += resultOfSubtraction + resultPtTwo;
}
}
return result;
}
double CalculateFuelForFuel(double num)
{
if (num <= 0)
return 0;
double fuelForFuel = 0;
double temp = DivideByThree(num);
temp -= 2;
fuelForFuel += temp;
if (fuelForFuel <=0)
return 0;
return fuelForFuel + CalculateFuelForFuel(fuelForFuel);
}
double DivideByThree(double asNumber)
{
return Convert.ToDouble(Math.Floor(asNumber / 3));
}
// Define other methods and classes here