2019 Day 01 solution

This commit is contained in:
Josh Creek
2019-12-01 11:21:45 +00:00
parent 7132535568
commit 14dd19c8d1
5 changed files with 239 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>Day_01</RootNamespace>
</PropertyGroup>
</Project>
+99
View File
@@ -0,0 +1,99 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace Day_01
{
class Program
{
static void Main(string[] args)
{
//Part1();
Part2();
//Test();
}
static void Part1()
{
// Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
var totalFuel = 0;
var lines = File.ReadAllLines("input.txt");
foreach (var line in lines)
{
var mass = Int32.Parse(line);
//divide by 3
decimal dividedAnswer = mass / 3;
// round down & subtract 2
var answer = Convert.ToInt32(Math.Floor(dividedAnswer)) - 2;
// add to total fuel count
totalFuel += answer;
}
// The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
// What is the sum of the fuel requirements for all of the modules on your spacecraft?
Console.WriteLine("Part 1: The sum of the fuel requirements for all of the modules on your spacecraft is " + totalFuel);
}
static int CalculateFuelNeeded(int mass)
{
//divide by 3
decimal dividedAnswer = mass / 3;
// round down & subtract 2
var answer = Convert.ToInt32(Math.Floor(dividedAnswer)) - 2;
// add to total fuel count
return answer;
}
static void Part2()
{
List<int> fuels = new List<int>();
var lines = File.ReadAllLines("input.txt");
foreach (var line in lines)
{
var mass = Int32.Parse(line);
// Calculate the fuel needed for the mass
var originalMassFuel = CalculateFuelNeeded(mass);
var nextFuel = CalculateFuelNeeded(originalMassFuel);
var totalFuel = originalMassFuel;
// Now calculate the fuel needed for the mass of the fuel, ad nauseum
// A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
// At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
var keepCalculating = true;
if(nextFuel > 0)
{
Console.WriteLine(nextFuel);
while(keepCalculating)
{
totalFuel += nextFuel;
nextFuel = CalculateFuelNeeded(nextFuel);
Console.WriteLine(nextFuel);
if(nextFuel <= 0)
{
keepCalculating = false;
}
}
}
fuels.Add(totalFuel);
}
// The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
int finalTotal = fuels.Sum(x => x);
// What is the sum of the fuel requirements for all of the modules on your spacecraft?
Console.WriteLine("Part 2: The sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel is " + finalTotal);
}
}
}
+20
View File
@@ -0,0 +1,20 @@
--- Day 1: The Tyranny of the Rocket Equation ---
Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
The Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
For example:
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
For a mass of 1969, the fuel required is 654.
For a mass of 100756, the fuel required is 33583.
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
What is the sum of the fuel requirements for all of the modules on your spacecraft?
+11
View File
@@ -0,0 +1,11 @@
--- Part Two ---
During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.
Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:
A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)
+100
View File
@@ -0,0 +1,100 @@
94794
58062
112067
139512
147400
99825
142617
107263
86294
97000
140204
72573
134981
111385
88303
79387
129111
122976
130685
75100
146566
73191
107641
109940
65518
102028
57370
144556
64017
64384
145114
115853
87939
90791
133443
139050
140657
85738
133749
92466
142918
96679
125035
127629
87906
104478
105147
121741
70312
73732
60838
82292
102931
103000
135903
78678
86314
50772
115673
106179
60615
105152
76550
140591
120916
62094
111273
63542
102974
78837
94840
89126
63150
52503
108530
101458
59660
116913
66440
83306
50693
58377
62005
130663
124304
79726
63001
73380
64395
124277
69742
63465
93172
142068
120081
119872
52801
100693
79229
90365