From a65d4fb1e8dc3c5690a2300bd89ff1dd0a4f7905 Mon Sep 17 00:00:00 2001 From: nin0dev Date: Mon, 2 Dec 2024 15:55:39 -0500 Subject: [PATCH] day 2 --- .gitignore | 7 +++- 2024/README.md | 2 +- 2024/day2/Program.cs | 79 +++++++++++++++++++++++++++++++++++++++++++ 2024/day2/example.txt | 6 ++++ 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 2024/day2/Program.cs create mode 100644 2024/day2/example.txt diff --git a/.gitignore b/.gitignore index 8a15a0c..1040fc5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ testing/ input.txt -.idea/ \ No newline at end of file +.idea/ +bin/ +obj/ +.vscode/ +*.sln +*.csproj \ No newline at end of file diff --git a/2024/README.md b/2024/README.md index 7910ee1..e72ef7f 100644 --- a/2024/README.md +++ b/2024/README.md @@ -14,7 +14,7 @@ AOC but one programming language per day - [ ] Dart - [ ] SQL - [ ] C -- [ ] C# +- [x] 2: C# (i get its meant to be used with visual studio but why make sooooo many meta files) - [x] 1: Kotlin (holy shit its actually a good lang) - [ ] PHP - [ ] JavaScript diff --git a/2024/day2/Program.cs b/2024/day2/Program.cs new file mode 100644 index 0000000..5e31a41 --- /dev/null +++ b/2024/day2/Program.cs @@ -0,0 +1,79 @@ +const int EXAMPLE_VALUE_1 = 2; +const int EXAMPLE_VALUE_2 = 4; + +StreamReader inputFile; +string input; + +Console.Write("Use (p)rod or (e)xample? > "); +if (Console.ReadKey().Key == ConsoleKey.E) { + Console.WriteLine($"\nUsing example data, value should be of {EXAMPLE_VALUE_1} and {EXAMPLE_VALUE_2}"); + inputFile = File.OpenText("example.txt"); +} else { + Console.WriteLine("\nUsing production data"); + inputFile = File.OpenText("input.txt"); +} +input = inputFile.ReadToEnd(); + +string[] reports = input.Split("\n"); +List invalidReports = []; +Console.WriteLine($"{reports.Length} reports will be processed"); + +int validReports = 0; + +static bool validateReport(List levels) { + bool shouldBeIncreasing = levels[0] < levels[1]; + bool hasIgnoredZeroIndex = false; + + int lastNumber = levels[0]; + foreach(var level in levels) { + if (levels.IndexOf(level) == 0 && !hasIgnoredZeroIndex) { + hasIgnoredZeroIndex = true; + continue; + } + + if (lastNumber >= level && shouldBeIncreasing || lastNumber <= level && !shouldBeIncreasing) return false; + if (Math.Abs(lastNumber - level) > 3) return false; + + lastNumber = level; + } + return true; +} + +// Part 1 +foreach(var report in reports) { + string[] levelsAsString = report.Split(" "); + List levels = []; + + foreach(var levelAsString in levelsAsString) { + levels.Add(int.Parse(levelAsString)); + } + if (validateReport(levels)) validReports++; + else { + invalidReports.Add(report); + } +} + +Console.WriteLine($"Part 1: {validReports} valid reports!"); + +// Part 2 +foreach(var report in invalidReports) { + string[] levelsAsString = report.Split(" "); + List levels = []; + + foreach(var levelAsString in levelsAsString) { + levels.Add(int.Parse(levelAsString)); + } + + for(var i = 0; i < levels.ToArray().Length; i++) { + List manipulatedLevels = new(levels); + manipulatedLevels.RemoveAt(i); + + if(validateReport(manipulatedLevels)) { + validReports++; + break; + } + } + +} + +Console.WriteLine($"Part 2: {validReports} valid reports!"); \ No newline at end of file diff --git a/2024/day2/example.txt b/2024/day2/example.txt new file mode 100644 index 0000000..82cd679 --- /dev/null +++ b/2024/day2/example.txt @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 \ No newline at end of file