Added actual new files....

This commit is contained in:
Rob 2024-12-01 10:17:24 +01:00
parent 0555dad92a
commit ec973d77cf
28 changed files with 1278 additions and 72 deletions

View File

@ -1,8 +1,8 @@
using AdventOfCode.Solutions;
using AdventOfCode.Core.Shared;
using AdventOfCode.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using AdventOfCode.Core.Shared.IO;
IChallange challange = Host.CreateDefaultBuilder()
@ -18,10 +18,8 @@ Console.WriteLine($"Part 2: {await challange.GetSolutionPart2()}");
static void ConfigureServices(IServiceCollection services)
{
services
.AddChallanges()
.AddTransient<InputReader, InputReader>()
.AddTransient<IInputReader, LocalFileReader>()
.AddScoped<SolutionManager>();
}

View File

@ -0,0 +1,31 @@
using AdventOfCode.Core.Shared.Grid;
namespace AdventOfCode.Core.Shared.IO
{
public interface IInputReader
{
int Day { get; set; }
int Year { get; set; }
void SetInput(IChallange challange);
void SetInput(int day, int year);
void SetSampleInput(bool isSample);
Task<string> ReadAsString();
IAsyncEnumerable<string> ReadAsStringLine();
Task<string[]> ReadAsArrayString();
Task<string[]> ReadAsVerticalArrayString();
IAsyncEnumerable<T> ReadAsStringLine<T>(T emptyLineIndicator);
IAsyncEnumerable<long[]> ReadLineAsLongArray(string seperator);
Task<Grid<T>> ReadToGrid<T>() where T : Point, new();
IAsyncEnumerable<Grid<T>> ReadToGrids<T>() where T : Node, new();
}
}

View File

@ -1,18 +1,17 @@
using AdventOfCode.Core.Shared;
using AdventOfCode.Core.Shared.Grid;
using AdventOfCode.Core.Shared.Grid;
namespace AdventOfCode.Core
namespace AdventOfCode.Core.Shared.IO
{
public class InputReader
public class LocalFileReader : IInputReader
{
private readonly string InputFileTemplate = "../../../../AdventOfCode.Solutions/{1}/Day {0:00}/day-{0:00}-input.txt";
private readonly string DebugInputFileTemplate = "../../../../AdventOfCode.Solutions/day-00-input.txt";
public bool IsDebug = false;
private int Day { get; set; }
private int Year { get; set; }
private string InputFilePath => IsDebug ? DebugInputFileTemplate : string.Format(InputFileTemplate, Day, Year);
public int Day { get; set; }
public int Year { get; set; }
public void SetInput(IChallange challange)
{
Day = challange.Day;
@ -25,6 +24,11 @@ namespace AdventOfCode.Core
Year = year;
}
public void SetSampleInput(bool isSample)
{
IsDebug = isSample;
}
public async Task<string> ReadAsString() => await File.ReadAllTextAsync(InputFilePath);
public async IAsyncEnumerable<string> ReadAsStringLine()
@ -32,7 +36,7 @@ namespace AdventOfCode.Core
using StreamReader reader = new(InputFilePath);
while (!reader.EndOfStream)
{
yield return (await reader.ReadLineAsync()) ?? string.Empty;
yield return await reader.ReadLineAsync() ?? string.Empty;
}
}
@ -51,14 +55,14 @@ namespace AdventOfCode.Core
}
return [.. result];
}
}
public async IAsyncEnumerable<T> ReadAsStringLine<T>(T emptyLineIndicator)
{
using StreamReader reader = new(InputFilePath);
while (!reader.EndOfStream)
{
string line = (await reader.ReadLineAsync()) ?? string.Empty;
string line = await reader.ReadLineAsync() ?? string.Empty;
if (string.IsNullOrWhiteSpace(line))
yield return emptyLineIndicator;
else
@ -71,7 +75,7 @@ namespace AdventOfCode.Core
using StreamReader reader = new(InputFilePath);
while (!reader.EndOfStream)
{
string line = (await reader.ReadLineAsync()) ?? string.Empty;
string line = await reader.ReadLineAsync() ?? string.Empty;
if (string.IsNullOrWhiteSpace(line))
yield return [];
else
@ -83,7 +87,7 @@ namespace AdventOfCode.Core
{
Grid<T> result = new();
int row = 0;
await foreach(string line in ReadAsStringLine())
await foreach (string line in ReadAsStringLine())
{
// create the nodes from the lines
result.DataGrid.AddRange(line.Select((c, i) => new T { X = i, Y = row, Value = c }));
@ -114,5 +118,7 @@ namespace AdventOfCode.Core
yield return result;
yield break;
}
}
}

View File

@ -0,0 +1,24 @@
using AdventOfCode.Core.Shared;
namespace AdventOfCode.Solutions
{
public class SolutionManager
{
private readonly IChallange[] _challanges;
public SolutionManager(IEnumerable<IChallange> challanges)
{
_challanges = challanges.ToArray();
}
public IEnumerable<IChallange> GetChallanges(Func<IChallange, bool> predicate)
{
return _challanges.Where(predicate);
}
public IChallange? GetChallange(int year, int day)
{
return _challanges.SingleOrDefault(challange => challange.Year == year && challange.Day == day);
}
}
}

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using AdventOfCode.Core.Shared.IO;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
@ -8,9 +9,9 @@ namespace AdventOfCode.Solutions._2023
public int Day => 1;
private readonly List<string> NumberMapping = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day01(InputReader inputReader)
public Day01(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using AdventOfCode.Core.Shared.IO;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 2;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day02(InputReader inputReader)
public Day02(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using AdventOfCode.Core.Shared.IO;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 3;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day03(InputReader inputReader)
public Day03(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,6 @@
namespace AdventOfCode.Solutions._2023
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public class Day04 : IChallange
{
@ -7,9 +9,9 @@
private Dictionary<int, int> _cardRuns = new Dictionary<int, int> { { 1, 1 } };
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day04(InputReader inputReader)
public Day04(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,6 @@
namespace AdventOfCode.Solutions._2023
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public class Day05 : IChallange
{
@ -13,9 +15,9 @@
public const string TemperatureHumidity = "temperature-to-humidity map:";
public const string HumidityLocation = "humidity-to-location map:";
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day05(InputReader inputReader)
public Day05(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using AdventOfCode.Core.Shared.IO;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 6;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day06(InputReader inputReader)
public Day06(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,6 @@
namespace AdventOfCode.Solutions._2023
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public class Day07 : IChallange
{
@ -9,9 +11,9 @@
private static readonly List<char> CardValuesJoker = ['J', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A'];
private enum SetRanks { High, OnePair, TwoPair, ThreeKind, FullHouse, FourKind, FiveKind }
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day07(InputReader inputReader)
public Day07(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using AdventOfCode.Core.Shared.IO;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 8;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day08(InputReader inputReader)
public Day08(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,13 +1,15 @@
namespace AdventOfCode.Solutions._2023
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public class Day09 : IChallange
{
public int Year => 2023;
public int Day => 9;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day09(InputReader inputReader)
public Day09(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,13 +1,15 @@
namespace AdventOfCode.Solutions._2023
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public class Day10 : IChallange
{
public int Year => 2023;
public int Day => 10;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day10(InputReader inputReader)
public Day10(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using AdventOfCode.Core.Shared.IO;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 11;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day11(InputReader inputReader)
public Day11(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,13 +1,15 @@
namespace AdventOfCode.Solutions._2023
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public partial class Day12 : IChallange
{
public int Year => 2023;
public int Day => 12;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day12(InputReader inputReader)
public Day12(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using AdventOfCode.Solutions._2023.Day_13;
using AdventOfCode.Core.Shared.IO;
using AdventOfCode.Solutions._2023.Day_13;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 13;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day13(InputReader inputReader)
public Day13(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using System.Text;
using AdventOfCode.Core.Shared.IO;
using System.Text;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 14;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day14(InputReader inputReader)
public Day14(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using System.Text;
using AdventOfCode.Core.Shared.IO;
using System.Text;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 15;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day15(InputReader inputReader)
public Day15(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,13 +1,15 @@
namespace AdventOfCode.Solutions._2023
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public class Day16 : IChallange
{
public int Year => 2023;
public int Day => 16;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day16(InputReader inputReader)
public Day16(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using AdventOfCode.Core.Shared.A_Star;
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 17;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day17(InputReader inputReader)
public Day17(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,13 +1,15 @@
namespace AdventOfCode.Solutions._2023
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public class Day18 : IChallange
{
public int Year => 2023;
public int Day => 18;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day18(InputReader inputReader)
public Day18(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using AdventOfCode.Core.Shared.IO;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
@ -7,9 +8,9 @@ namespace AdventOfCode.Solutions._2023
public int Year => 2023;
public int Day => 19;
private readonly InputReader _inputReader;
private readonly IInputReader _inputReader;
public Day19(InputReader inputReader)
public Day19(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);

View File

@ -0,0 +1,69 @@
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2024
{
public class Day01 : IChallange
{
public int Year => 2024;
public int Day => 1;
private readonly IInputReader _inputReader;
public Day01(IInputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);
}
//11
//1889772
public async Task<string> GetSolutionPart1()
{
int total = 0;
(List<int> One, List<int> Two) = await GetLists();
One = [.. One.Order()];
Two = [.. Two.Order()];
for (int index = 0; index < One.Count; index++) {
int distance = Math.Abs(One[index] - Two[index]);
total += distance;
}
return total.ToString();
}
//31
//23228917
public async Task<string> GetSolutionPart2()
{
int total = 0;
(List<int> One, List<int> Two) = await GetLists();
var oneGroup = One.GroupBy(x => x);
var twoGroup = Two.GroupBy(x => x);
foreach(var group in oneGroup)
{
var grouptwo = twoGroup.FirstOrDefault(g => g.Key == group.Key)?.Count() ?? 0;
total += group.Key * group.Count() * grouptwo;
}
return total.ToString();
}
private async Task<(List<int> One, List<int> Two)> GetLists()
{
string[] data = await _inputReader.ReadAsArrayString();
List<int> one = [], two = [];
foreach (string s in data)
{
int[] numbers = s.Split(" ").Select(int.Parse).ToArray();
one.Add(numbers[0]);
two.Add(numbers[1]);
}
return (one, two);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,28 @@
using AdventOfCode.Core.Shared.IO;
namespace AdventOfCode.Solutions._2023
{
public class Day00 : IChallange
{
public int Year => 0;
public int Day => 0;
private readonly InputReader _inputReader;
public Day00(InputReader inputReader)
{
_inputReader = inputReader;
_inputReader.SetInput(this);
}
public async Task<string> GetSolutionPart1()
{
return string.Empty;
}
public async Task<string> GetSolutionPart2()
{
return string.Empty;
}
}
}

View File

@ -0,0 +1,23 @@
using AdventOfCode.Solutions._2024;
using Microsoft.Extensions.DependencyInjection;
namespace AdventOfCode.Solutions
{
public static class ServiceConfiguration
{
public static IServiceCollection AddChallanges(this IServiceCollection services)
{
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);
//}
return services;
}
}
}