|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace adventofcode.Year2019.Day01; |
| 4 | + |
| 5 | +public class Solution : BaseSolution, ISolver |
| 6 | +{ |
| 7 | + public void SolvePart1() |
| 8 | + { |
| 9 | + string line; |
| 10 | + //int counter = 0; |
| 11 | + int neededfuel = 0; |
| 12 | + // string filepath = Environment.CurrentDirectory + @"\2019\Day01\201901_1.txt"; |
| 13 | + //string filepath = Path.Combine(Environment.CurrentDirectory, "2019", "Day01", "201901_1.txt"); |
| 14 | + //string filepath = Path.Combine(Environment.CurrentDirectory, GetYear(), $"Day{GetDay()}", "input.txt"); |
| 15 | + string filepath = GetInputFilePath("201901_1.txt"); |
| 16 | + |
| 17 | + Console.WriteLine(filepath); |
| 18 | + |
| 19 | + if(System.IO.File.Exists(filepath)) |
| 20 | + { |
| 21 | + System.IO.StreamReader file = new System.IO.StreamReader(filepath); |
| 22 | + |
| 23 | + while((line = file.ReadLine()) != null) |
| 24 | + { |
| 25 | + int mass = Convert.ToInt32(line); |
| 26 | + int fuel = mass / 3 - 2; |
| 27 | + |
| 28 | + neededfuel = neededfuel + fuel; |
| 29 | + //Console.WriteLine(line); |
| 30 | + //counter++; |
| 31 | + } |
| 32 | + //Console.WriteLine("{0} lines read.", counter.ToString()); |
| 33 | + Console.WriteLine("we need {0} fuel.", neededfuel); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public void SolvePart2() |
| 38 | + { |
| 39 | + string line; |
| 40 | + //int counter = 0; |
| 41 | + int neededfuel = 0; |
| 42 | + //string filepath = Environment.CurrentDirectory + @"\2019\Day01\201901_1.txt"; |
| 43 | + string filepath = System.IO.Path.Combine(Environment.CurrentDirectory, "2019", "Day01", "201901_1.txt"); |
| 44 | + |
| 45 | + Console.WriteLine(filepath); |
| 46 | + |
| 47 | + if(System.IO.File.Exists(filepath)) |
| 48 | + { |
| 49 | + System.IO.StreamReader file = new System.IO.StreamReader(filepath); |
| 50 | + |
| 51 | + while((line = file.ReadLine()) != null) |
| 52 | + { |
| 53 | + int mass = Convert.ToInt32(line); |
| 54 | + int fuel = mass / 3 - 2; |
| 55 | + int fueltotal = fuel; |
| 56 | + |
| 57 | + while(fuel / 3 - 2 > 0) |
| 58 | + { |
| 59 | + fuel = fuel / 3 - 2; |
| 60 | + fueltotal = fueltotal + fuel; |
| 61 | + } |
| 62 | + |
| 63 | + neededfuel = neededfuel + fueltotal; |
| 64 | + |
| 65 | + //Console.WriteLine(line); |
| 66 | + //counter++; |
| 67 | + } |
| 68 | + //Console.WriteLine("{0} lines read.", counter.ToString()); |
| 69 | + Console.WriteLine("we need {0} fuel.", neededfuel); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments