Added Day 2 part 1

This commit is contained in:
Rob Stoffelen 2024-12-02 08:45:56 +01:00
parent ec973d77cf
commit 2c0ba40a24
7 changed files with 1109 additions and 16 deletions

View File

@ -10,7 +10,7 @@ IChallange challange = Host.CreateDefaultBuilder()
.Build()
.Services
.GetService<SolutionManager>()
.GetChallange(2024, 1);
.GetChallange(2024, 2);
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");

View File

@ -0,0 +1,93 @@
using AdventOfCode.Core.Shared.IO;
using System;
using System.Reflection;
namespace AdventOfCode.Solutions._2024
{
public class Day02 : IChallange
{
public int Year => 2024;
public int Day => 2;
private readonly IInputReader _inputReader;
public Day02(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);
}
//2
//
public async Task<string> GetSolutionPart1()
{
//_inputReader.SetSampleInput(true);
string[] data = await _inputReader.ReadAsArrayString();
int save = data.Length;
foreach (string line in data)
{
int[] intData = line.Split(' ').Select(int.Parse).ToArray();
bool isIncrease = intData[1] < intData[0];
for (int index = 1; index < intData.Length; index++)
{
int trueDiff = intData[index - 1] - intData[index];
int diff = Math.Abs(trueDiff);
if (diff < 1 || diff > 3)
{
save--;
break;
}
if (isIncrease && trueDiff < 0)
{
save--;
break;
}
else if (!isIncrease && trueDiff > 0)
{
save--;
break;
}
}
}
return save.ToString();
}
//
//
public async Task<string> GetSolutionPart2()
{
string[] data = await _inputReader.ReadAsArrayString();
int save = data.Length;
foreach (string line in data)
{
int[] intData = line.Split(' ').Select(int.Parse).ToArray();
bool isIncrease = intData[1] < intData[0];
for (int index = 1; index < intData.Length; index++)
{
int trueDiff = intData[index - 1] - intData[index];
int diff = Math.Abs(trueDiff);
if (diff < 1 || diff > 3)
{
save--;
break;
}
if (isIncrease && trueDiff < 0)
{
save--;
break;
}
else if (!isIncrease && trueDiff > 0)
{
save--;
break;
}
}
}
return save.ToString();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -34,6 +34,9 @@
<Compile Update="2023\Day 13\Day13.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2024\Day 02\Day02.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="Day 00\Day00.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>

View File

@ -8,12 +8,11 @@ namespace AdventOfCode.Solutions._2023
public int Day => 0;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day00(InputReader inputReader)
public Day00(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);
}
public async Task<string> GetSolutionPart1()
{

View File

@ -10,12 +10,10 @@ namespace AdventOfCode.Solutions
IEnumerable<Type> challanges = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
.Where(t => t.GetInterfaces().Contains(typeof(IChallange)));
services.AddScoped<IChallange, Day01>();
//foreach (Type challange in challanges)
//{
// services.AddScoped(typeof(IChallange), challange);
//}
foreach (Type challange in challanges)
{
services.AddScoped(typeof(IChallange), challange);
}
return services;
}

View File

@ -1,6 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
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