1
1
Fork 0
This commit is contained in:
nin0dev 2024-12-02 15:55:39 -05:00
parent af90d44169
commit a65d4fb1e8
Signed by: nin0
SSH key fingerprint: SHA256:Is2DvJdw1OkSopR4wKJfdWV0fZhMLxpnCs1P1nPhIgA
4 changed files with 92 additions and 2 deletions

5
.gitignore vendored
View file

@ -1,3 +1,8 @@
testing/ testing/
input.txt input.txt
.idea/ .idea/
bin/
obj/
.vscode/
*.sln
*.csproj

View file

@ -14,7 +14,7 @@ AOC but one programming language per day
- [ ] Dart - [ ] Dart
- [ ] SQL - [ ] SQL
- [ ] C - [ ] 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) - [x] 1: Kotlin (holy shit its actually a good lang)
- [ ] PHP - [ ] PHP
- [ ] JavaScript - [ ] JavaScript

79
2024/day2/Program.cs Normal file
View file

@ -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<string> invalidReports = [];
Console.WriteLine($"{reports.Length} reports will be processed");
int validReports = 0;
static bool validateReport(List<int> 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 <int> 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 <int> levels = [];
foreach(var levelAsString in levelsAsString) {
levels.Add(int.Parse(levelAsString));
}
for(var i = 0; i < levels.ToArray().Length; i++) {
List<int> manipulatedLevels = new(levels);
manipulatedLevels.RemoveAt(i);
if(validateReport(manipulatedLevels)) {
validReports++;
break;
}
}
}
Console.WriteLine($"Part 2: {validReports} valid reports!");

6
2024/day2/example.txt Normal file
View file

@ -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