Added Day 2 part 1
This commit is contained in:
parent
ec973d77cf
commit
2c0ba40a24
@ -10,7 +10,7 @@ IChallange challange = Host.CreateDefaultBuilder()
|
|||||||
.Build()
|
.Build()
|
||||||
.Services
|
.Services
|
||||||
.GetService<SolutionManager>()
|
.GetService<SolutionManager>()
|
||||||
.GetChallange(2024, 1);
|
.GetChallange(2024, 2);
|
||||||
|
|
||||||
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
||||||
|
|
||||||
|
|||||||
93
AdventOfCode.Solutions/2024/Day 02/Day02.cs
Normal file
93
AdventOfCode.Solutions/2024/Day 02/Day02.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1000
AdventOfCode.Solutions/2024/Day 02/day-02-input.txt
Normal file
1000
AdventOfCode.Solutions/2024/Day 02/day-02-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -34,6 +34,9 @@
|
|||||||
<Compile Update="2023\Day 13\Day13.cs">
|
<Compile Update="2023\Day 13\Day13.cs">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Update="2024\Day 02\Day02.cs">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Compile>
|
||||||
<Compile Update="Day 00\Day00.cs">
|
<Compile Update="Day 00\Day00.cs">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
@ -8,12 +8,11 @@ namespace AdventOfCode.Solutions._2023
|
|||||||
|
|
||||||
public int Day => 0;
|
public int Day => 0;
|
||||||
|
|
||||||
private readonly InputReader _inputReader;
|
private readonly IInputReader _inputReader;
|
||||||
|
|
||||||
public Day00(InputReader inputReader)
|
public Day00(IInputReader inputReader)
|
||||||
{
|
{
|
||||||
_inputReader = inputReader;
|
_inputReader = inputReader;
|
||||||
_inputReader.SetInput(this);
|
|
||||||
}
|
}
|
||||||
public async Task<string> GetSolutionPart1()
|
public async Task<string> GetSolutionPart1()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -10,12 +10,10 @@ namespace AdventOfCode.Solutions
|
|||||||
IEnumerable<Type> challanges = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
|
IEnumerable<Type> challanges = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
|
||||||
.Where(t => t.GetInterfaces().Contains(typeof(IChallange)));
|
.Where(t => t.GetInterfaces().Contains(typeof(IChallange)));
|
||||||
|
|
||||||
services.AddScoped<IChallange, Day01>();
|
foreach (Type challange in challanges)
|
||||||
|
{
|
||||||
//foreach (Type challange in challanges)
|
services.AddScoped(typeof(IChallange), challange);
|
||||||
//{
|
}
|
||||||
// services.AddScoped(typeof(IChallange), challange);
|
|
||||||
//}
|
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
3 4
|
7 6 4 2 1
|
||||||
4 3
|
1 2 7 8 9
|
||||||
2 5
|
9 7 6 2 1
|
||||||
1 3
|
1 3 2 4 5
|
||||||
3 9
|
8 6 4 4 1
|
||||||
3 3
|
1 3 6 7 9
|
||||||
Loading…
Reference in New Issue
Block a user