Add day 2

This commit is contained in:
Dimitrios Kaltzidis
2024-12-02 09:45:28 +02:00
parent 2df05c9d05
commit d49d232a20
4 changed files with 1064 additions and 1 deletions

View File

@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102 VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day1", "Day1\Day1.csproj", "{B9B134C5-93CB-4F54-90F6-FA911A04BDA5}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day1", "Day1\Day1.csproj", "{B9B134C5-93CB-4F54-90F6-FA911A04BDA5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day2", "Day2\Day2.csproj", "{4C4D9A68-A4E2-4912-BFE4-54080736D611}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
{B9B134C5-93CB-4F54-90F6-FA911A04BDA5}.Debug|Any CPU.Build.0 = Debug|Any CPU {B9B134C5-93CB-4F54-90F6-FA911A04BDA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9B134C5-93CB-4F54-90F6-FA911A04BDA5}.Release|Any CPU.ActiveCfg = Release|Any CPU {B9B134C5-93CB-4F54-90F6-FA911A04BDA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9B134C5-93CB-4F54-90F6-FA911A04BDA5}.Release|Any CPU.Build.0 = Release|Any CPU {B9B134C5-93CB-4F54-90F6-FA911A04BDA5}.Release|Any CPU.Build.0 = Release|Any CPU
{4C4D9A68-A4E2-4912-BFE4-54080736D611}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C4D9A68-A4E2-4912-BFE4-54080736D611}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C4D9A68-A4E2-4912-BFE4-54080736D611}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C4D9A68-A4E2-4912-BFE4-54080736D611}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

16
Day2/Day2.csproj Normal file
View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Update="input.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

41
Day2/Program.cs Normal file
View File

@@ -0,0 +1,41 @@
var lines = File.ReadAllLines("input.txt");
var reports = lines.Select(line => line.Split(" ").ToList()).ToList();
int safeReportsNumber = 0, problemDampenerSafeReports = 0;
for (int i = 0; i < reports.Count; i++)
{
var currentReport = reports[i].Select(x => int.Parse(x)).ToList();
if (IsReportSafe(currentReport)) safeReportsNumber++;
if (IsReportSafeWithDampener(currentReport)) problemDampenerSafeReports++;
}
Console.WriteLine($"Part1: {safeReportsNumber}\nPart2: {problemDampenerSafeReports}");
bool IsReportSafe(List<int> levels)
{
var isAscending = levels.SequenceEqual(levels.OrderBy(x => x));
var isDescending = levels.SequenceEqual(levels.OrderByDescending(x => x));
var allValidDifferennce = levels.Zip(levels.Skip(1), (a, b) => Math.Abs(a - b) > 0 && Math.Abs(a - b) < 4).All(x => x);
return (isAscending || isDescending) && allValidDifferennce;
}
bool IsReportSafeWithDampener(List<int> levels)
{
if (IsReportSafe(levels)) return true;
// Performance killa..
for (int i = 0; i < levels.Count; i++)
{
var modifiedLevels = new List<int>(levels);
modifiedLevels.RemoveAt(i);
if (IsReportSafe(modifiedLevels)) return true;
}
return false;
}

1000
Day2/input.txt Normal file

File diff suppressed because it is too large Load Diff