From 127a57221acb80a12bde6fa964defba62ee6209f Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Mon, 2 Dec 2019 20:58:03 -0500 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ Advent.csproj | 8 +++++++ Advent.sln | 25 +++++++++++++++++++++ Day1.cs | 47 +++++++++++++++++++++++++++++++++++++++ Day2.cs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ Program.cs | 10 +++++++++ README.md | 3 ++- 7 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Advent.csproj create mode 100644 Advent.sln create mode 100644 Day1.cs create mode 100644 Day2.cs create mode 100644 Program.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd1d906 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs +bin +obj \ No newline at end of file diff --git a/Advent.csproj b/Advent.csproj new file mode 100644 index 0000000..958d2f1 --- /dev/null +++ b/Advent.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.0 + + + diff --git a/Advent.sln b/Advent.sln new file mode 100644 index 0000000..d3b2d86 --- /dev/null +++ b/Advent.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29519.87 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Advent", "Advent.csproj", "{6D876526-1EA3-484C-B1B2-A6EEFEF7CA23}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6D876526-1EA3-484C-B1B2-A6EEFEF7CA23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D876526-1EA3-484C-B1B2-A6EEFEF7CA23}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D876526-1EA3-484C-B1B2-A6EEFEF7CA23}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D876526-1EA3-484C-B1B2-A6EEFEF7CA23}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1881BC49-072A-4A7E-9C3B-8172112237CC} + EndGlobalSection +EndGlobal diff --git a/Day1.cs b/Day1.cs new file mode 100644 index 0000000..f88a52b --- /dev/null +++ b/Day1.cs @@ -0,0 +1,47 @@ +using System; +using System.IO; + +namespace Advent +{ + public static class Day1 + { + public static void Execute() + { + var lines = File.ReadAllLines(@"C:\Users\chris\OneDrive\Desktop\input.txt"); + + var total = 0.0; + + foreach (var line in lines) + { + var mass = double.Parse(line); + + var fuel = CalculateFuel(mass); + + total += fuel; + } + + Console.WriteLine(total); + } + + private static double CalculateFuel(double mass) + { + var totalFuel = 0.0; + + var fuel = Math.Floor(mass / 3) - 2; + + totalFuel += fuel; + + while (fuel > 2) + { + fuel = Math.Floor(fuel / 3) - 2; + + if (fuel < 0) + fuel = 0; + + totalFuel += fuel; + } + + return totalFuel; + } + } +} diff --git a/Day2.cs b/Day2.cs new file mode 100644 index 0000000..55358d9 --- /dev/null +++ b/Day2.cs @@ -0,0 +1,61 @@ +using System; +using System.IO; +using System.Linq; + +namespace Advent +{ + public static class Day2 + { + public static void Execute() + { + for (var noun = 0; noun <= 99; noun++) + for (var verb = 0; verb <= 99; verb++) + { + var result = Calculate(noun, verb); + + if (result == 19690720) + Console.WriteLine(100 * noun + verb); + } + } + + private static int Calculate(int noun, int verb) + { + var lines = File.ReadAllLines(@"C:\Users\chris\OneDrive\Desktop\input.txt"); + + var codes = lines[0].Split(',').Select(int.Parse).ToArray(); + + codes[1] = noun; + codes[2] = verb; + + var position = 0; + var done = false; + + while (!done) + { + switch (codes[position]) + { + case 1: + codes[codes[position + 3]] = codes[codes[position + 1]] + codes[codes[position + 2]]; + + position += 4; + + break; + + case 2: + codes[codes[position + 3]] = codes[codes[position + 1]] * codes[codes[position + 2]]; + + position += 4; + + break; + + case 99: + done = true; + + break; + } + } + + return codes[0]; + } + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..8d31c39 --- /dev/null +++ b/Program.cs @@ -0,0 +1,10 @@ +namespace Advent +{ + internal static class Program + { + private static void Main() + { + Day2.Execute(); + } + } +} diff --git a/README.md b/README.md index de6b52b..5c27c63 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # Advent2019 -https://adventofcode.com/ + +My son wanted me to commit my code for [Advent of Code 2019](https://adventofcode.com/) so he could compare it to his own. \ No newline at end of file