Reworked underlying code, added day 4
This commit is contained in:
parent
89486fde35
commit
b41ac3c81b
@ -1,6 +1,16 @@
|
|||||||
using Day_03.Day_03;
|
using AdventOfCodeLibrary._2022;
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
byte[] dataArray = File.ReadAllBytes(Day03.InputPath);
|
string _demoData = @"2-4,6-8
|
||||||
|
2-3,4-5
|
||||||
|
5-7,7-9
|
||||||
|
2-8,3-7
|
||||||
|
6-6,4-6
|
||||||
|
2-6,4-8";
|
||||||
|
|
||||||
Console.WriteLine($"Max value: {Day03.GetPart2(dataArray)}");
|
IAnswerable answerable = new Day04Part1();
|
||||||
|
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||||
|
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||||
|
|
||||||
|
Console.WriteLine($"Max value: {answerable.GetAwner(dataArray)}");
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
@ -1,29 +0,0 @@
|
|||||||
namespace Day_01.Day_01
|
|
||||||
{
|
|
||||||
public class Day01
|
|
||||||
{
|
|
||||||
private static string inputPath = "Day 01/day-01-input.txt";
|
|
||||||
|
|
||||||
public static int GetPart1()
|
|
||||||
{
|
|
||||||
return File.ReadAllText(inputPath)
|
|
||||||
.TrimEnd()
|
|
||||||
.Split(Environment.NewLine + Environment.NewLine)
|
|
||||||
.Select(s => s.Split(Environment.NewLine)
|
|
||||||
.Sum(Convert.ToInt32))
|
|
||||||
.MaxBy(v => v);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int GetPart2()
|
|
||||||
{
|
|
||||||
return File.ReadAllText(inputPath)
|
|
||||||
.TrimEnd()
|
|
||||||
.Split(Environment.NewLine + Environment.NewLine)
|
|
||||||
.Select(s => s.Split(Environment.NewLine)
|
|
||||||
.Sum(Convert.ToInt32))
|
|
||||||
.OrderByDescending(v => v)
|
|
||||||
.Take(3)
|
|
||||||
.Sum();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
21
Advent Of Code Library/2022/Day 01/Day01Part1.cs
Normal file
21
Advent Of Code Library/2022/Day 01/Day01Part1.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day01Part1 : IAnswerable
|
||||||
|
{
|
||||||
|
public int Year { get; set; } = 2022;
|
||||||
|
public int Day { get; set; } = 1;
|
||||||
|
public int Part { get; set; } = 1;
|
||||||
|
|
||||||
|
public string GetAwner(byte[] data)
|
||||||
|
{
|
||||||
|
return ByteHelper.GetAsString(data).TrimEnd()
|
||||||
|
.Split(Environment.NewLine + Environment.NewLine)
|
||||||
|
.Select(s => s.Split(Environment.NewLine)
|
||||||
|
.Sum(Convert.ToInt32))
|
||||||
|
.MaxBy(v => v)
|
||||||
|
.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
Advent Of Code Library/2022/Day 01/Day01Part2.cs
Normal file
25
Advent Of Code Library/2022/Day 01/Day01Part2.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day01Part2 : IAnswerable
|
||||||
|
{
|
||||||
|
public int Year { get; set; } = 2022;
|
||||||
|
public int Day { get; set; } = 1;
|
||||||
|
public int Part { get; set; } = 2;
|
||||||
|
public string DefaultInputFile { get; set; } = "Day 01/day-01-input.txt";
|
||||||
|
|
||||||
|
public string GetAwner(byte[] data)
|
||||||
|
{
|
||||||
|
return ByteHelper.GetAsString(data).TrimEnd()
|
||||||
|
.TrimEnd()
|
||||||
|
.Split(Environment.NewLine + Environment.NewLine)
|
||||||
|
.Select(s => s.Split(Environment.NewLine)
|
||||||
|
.Sum(Convert.ToInt32))
|
||||||
|
.OrderByDescending(v => v)
|
||||||
|
.Take(3)
|
||||||
|
.Sum()
|
||||||
|
.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
Advent Of Code Library/2022/Day 02/Day02Part1.cs
Normal file
21
Advent Of Code Library/2022/Day 02/Day02Part1.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary._2022.Day02;
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day02Part1 : IAnswerable
|
||||||
|
{
|
||||||
|
public int Year { get; set; } = 2022;
|
||||||
|
public int Day { get; set; } = 2;
|
||||||
|
public int Part { get; set; } = 1;
|
||||||
|
|
||||||
|
public string GetAwner(byte[] data)
|
||||||
|
{
|
||||||
|
GameRules.BuildScoreDic();
|
||||||
|
return ByteHelper.GetAsStringArray(data)
|
||||||
|
.Select(GameRules.GetScore)
|
||||||
|
.Sum()
|
||||||
|
.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
Advent Of Code Library/2022/Day 02/Day02Part2.cs
Normal file
21
Advent Of Code Library/2022/Day 02/Day02Part2.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary._2022.Day02;
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day02Part2 : IAnswerable
|
||||||
|
{
|
||||||
|
public int Year { get; set; } = 2022;
|
||||||
|
public int Day { get; set; } = 2;
|
||||||
|
public int Part { get; set; } = 2;
|
||||||
|
|
||||||
|
public string GetAwner(byte[] data)
|
||||||
|
{
|
||||||
|
GameRules.BuildWinDrawLoseTable();
|
||||||
|
return ByteHelper.GetAsStringArray(data)
|
||||||
|
.Select(GameRules.GetScore)
|
||||||
|
.Sum()
|
||||||
|
.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,18 +1,16 @@
|
|||||||
namespace Day_02.Day_02
|
namespace AdventOfCodeLibrary._2022.Day02
|
||||||
{
|
{
|
||||||
public class Day02
|
internal class GameRules
|
||||||
{
|
{
|
||||||
private static string inputPath = "Day 02/day-02-input.txt";
|
|
||||||
|
|
||||||
private static int Win = 6, Draw = 3, Lost = 0, Rock = 1, Paper = 2, Scissors = 3;
|
private static int Win = 6, Draw = 3, Lost = 0, Rock = 1, Paper = 2, Scissors = 3;
|
||||||
|
|
||||||
private static string OpponentRock = "A", OpponentPaper = "B", OpponentScissors = "C",
|
private static string OpponentRock = "A", OpponentPaper = "B", OpponentScissors = "C",
|
||||||
PlayRock = "X", PlayPaper = "Y", PlayScissors = "Z",
|
PlayRock = "X", PlayPaper = "Y", PlayScissors = "Z",
|
||||||
PlayWin = "Z", PlayDraw = "Y", PlayLose = "X";
|
PlayWin = "Z", PlayDraw = "Y", PlayLose = "X";
|
||||||
|
|
||||||
private static Dictionary<string, int> PossibleScores;
|
private static Dictionary<string, int> PossibleScores { get; set; }
|
||||||
|
|
||||||
private static void BuildScoreDic()
|
internal static void BuildScoreDic()
|
||||||
{
|
{
|
||||||
if (PossibleScores == null)
|
if (PossibleScores == null)
|
||||||
{
|
{
|
||||||
@ -36,7 +34,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void BuildWinDrawLoseTable()
|
internal static void BuildWinDrawLoseTable()
|
||||||
{
|
{
|
||||||
if (PossibleScores == null)
|
if (PossibleScores == null)
|
||||||
{
|
{
|
||||||
@ -60,22 +58,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int GetScore(string input) => PossibleScores[input];
|
internal static int GetScore(string play) => PossibleScores[play];
|
||||||
|
|
||||||
public static int GetPart1()
|
|
||||||
{
|
|
||||||
BuildScoreDic();
|
|
||||||
return File.ReadAllLines(inputPath)
|
|
||||||
.Select(GetScore)
|
|
||||||
.Sum();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int GetPart2()
|
|
||||||
{
|
|
||||||
BuildWinDrawLoseTable();
|
|
||||||
return File.ReadAllLines(inputPath)
|
|
||||||
.Select(GetScore)
|
|
||||||
.Sum();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,43 +0,0 @@
|
|||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Day_03.Day_03
|
|
||||||
{
|
|
||||||
public class Day03
|
|
||||||
{
|
|
||||||
public static string InputPath = "Day 03/day-03-input.txt";
|
|
||||||
|
|
||||||
public static string GetPart1(byte[] data)
|
|
||||||
{
|
|
||||||
string[] rucksackData = Encoding.UTF8.GetString(data).Split(Environment.NewLine);
|
|
||||||
int priorityCount = 0;
|
|
||||||
|
|
||||||
// go throu each ruchsack
|
|
||||||
foreach (string rucksack in rucksackData.Where(x => !string.IsNullOrWhiteSpace(x)))
|
|
||||||
{
|
|
||||||
// split the data line down the middel
|
|
||||||
string rucksackOne = rucksack.Substring(0, rucksack.Length / 2);
|
|
||||||
string rucksackTwo = rucksack.Substring(rucksack.Length / 2, rucksack.Length / 2);
|
|
||||||
char prioChar = rucksackOne.Where(x => rucksackTwo.Contains(x)).First();
|
|
||||||
priorityCount += prioChar > 'Z' ? (prioChar - 'a' + 1) : (prioChar - 'A' + 27);
|
|
||||||
}
|
|
||||||
|
|
||||||
return priorityCount.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string GetPart2(byte[] data)
|
|
||||||
{
|
|
||||||
string[] rucksackData = Encoding.UTF8.GetString(data).Split(Environment.NewLine);
|
|
||||||
int priorityCount = 0;
|
|
||||||
|
|
||||||
for (int index = 0; index < rucksackData.Length; index += 3)
|
|
||||||
{
|
|
||||||
// get the 3 ruchsacks
|
|
||||||
string[] ruchsacks = rucksackData.Skip(index).Take(3).ToArray();
|
|
||||||
char prioChar = ruchsacks[0].Where(x => ruchsacks[1].Contains(x) && ruchsacks[2].Contains(x)).First();
|
|
||||||
priorityCount += prioChar > 'Z' ? (prioChar - 'a' + 1) : (prioChar - 'A' + 27);
|
|
||||||
}
|
|
||||||
|
|
||||||
return priorityCount.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
29
Advent Of Code Library/2022/Day 03/Day03Part1.cs
Normal file
29
Advent Of Code Library/2022/Day 03/Day03Part1.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day03Part1 : IAnswerable
|
||||||
|
{
|
||||||
|
public int Year { get; set; } = 2022;
|
||||||
|
public int Day { get; set; } = 3;
|
||||||
|
public int Part { get; set; } = 1;
|
||||||
|
|
||||||
|
public string GetAwner(byte[] data)
|
||||||
|
{
|
||||||
|
string[] rucksackData = ByteHelper.GetAsStringArray(data);
|
||||||
|
int priorityCount = 0;
|
||||||
|
|
||||||
|
// go throu each ruchsack
|
||||||
|
foreach (string rucksack in rucksackData.Where(x => !string.IsNullOrWhiteSpace(x)))
|
||||||
|
{
|
||||||
|
// split the data line down the middel
|
||||||
|
string rucksackOne = rucksack.Substring(0, rucksack.Length / 2);
|
||||||
|
string rucksackTwo = rucksack.Substring(rucksack.Length / 2, rucksack.Length / 2);
|
||||||
|
char prioChar = rucksackOne.Where(x => rucksackTwo.Contains(x)).First();
|
||||||
|
priorityCount += prioChar > 'Z' ? (prioChar - 'a' + 1) : (prioChar - 'A' + 27);
|
||||||
|
}
|
||||||
|
|
||||||
|
return priorityCount.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
Advent Of Code Library/2022/Day 03/Day03Part2.cs
Normal file
28
Advent Of Code Library/2022/Day 03/Day03Part2.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day03Part2 : IAnswerable
|
||||||
|
{
|
||||||
|
public int Year { get; set; } = 2022;
|
||||||
|
public int Day { get; set; } = 3;
|
||||||
|
public int Part { get; set; } = 2;
|
||||||
|
|
||||||
|
public string GetAwner(byte[] data)
|
||||||
|
{
|
||||||
|
string[] rucksackData = ByteHelper.GetAsStringArray(data);
|
||||||
|
int priorityCount = 0;
|
||||||
|
|
||||||
|
// go throu each ruchsack
|
||||||
|
for (int index = 0; index < rucksackData.Length; index += 3)
|
||||||
|
{
|
||||||
|
// get the 3 ruchsacks
|
||||||
|
string[] ruchsacks = rucksackData.Skip(index).Take(3).ToArray();
|
||||||
|
char prioChar = ruchsacks[0].Where(x => ruchsacks[1].Contains(x) && ruchsacks[2].Contains(x)).First();
|
||||||
|
priorityCount += prioChar > 'Z' ? (prioChar - 'a' + 1) : (prioChar - 'A' + 27);
|
||||||
|
}
|
||||||
|
|
||||||
|
return priorityCount.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
Advent Of Code Library/2022/Day 04/Day04Part1.cs
Normal file
28
Advent Of Code Library/2022/Day 04/Day04Part1.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day04Part1 : IAnswerable
|
||||||
|
{
|
||||||
|
public int Year { get; set; } = 2022;
|
||||||
|
public int Day { get; set; } = 4;
|
||||||
|
public int Part { get; set; } = 1;
|
||||||
|
|
||||||
|
public string GetAwner(byte[] data)
|
||||||
|
{
|
||||||
|
return ByteHelper.GetAsStringArray(data)
|
||||||
|
.Where(line => !string.IsNullOrWhiteSpace(line))
|
||||||
|
.Select(line =>
|
||||||
|
line.Split(',')
|
||||||
|
.Select(s => s.Trim())
|
||||||
|
.Select(s => s.Split('-')
|
||||||
|
.Select(s => Convert.ToInt32(s))
|
||||||
|
.ToArray())
|
||||||
|
.OrderBy(s => s[0])
|
||||||
|
.OrderByDescending(s => s[1])
|
||||||
|
.ToArray())
|
||||||
|
.Count(sections => sections[0][0] <= sections[1][0] && sections[0][1] >= sections[1][1])
|
||||||
|
.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
Advent Of Code Library/2022/Day 04/Day04Part2.cs
Normal file
27
Advent Of Code Library/2022/Day 04/Day04Part2.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day04Part2 : IAnswerable
|
||||||
|
{
|
||||||
|
public int Year { get; set; } = 2022;
|
||||||
|
public int Day { get; set; } = 4;
|
||||||
|
public int Part { get; set; } = 2;
|
||||||
|
|
||||||
|
public string GetAwner(byte[] data)
|
||||||
|
{
|
||||||
|
return ByteHelper.GetAsStringArray(data)
|
||||||
|
.Where(line => !string.IsNullOrWhiteSpace(line))
|
||||||
|
.Select(line =>
|
||||||
|
line.Split(',')
|
||||||
|
.Select(s => s.Trim())
|
||||||
|
.Select(s => s.Split('-')
|
||||||
|
.Select(s => Convert.ToInt32(s))
|
||||||
|
.ToArray())
|
||||||
|
.OrderBy(s => s[0])
|
||||||
|
.ToArray())
|
||||||
|
.Count(sections => sections[0][1] >= sections[1][0])
|
||||||
|
.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1000
Advent Of Code Library/2022/Day 04/day-04-input.txt
Normal file
1000
Advent Of Code Library/2022/Day 04/day-04-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<RootNamespace>Advent_Of_Code_Library</RootNamespace>
|
<RootNamespace>AdventOfCodeLibrary</RootNamespace>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
26
Advent Of Code Library/Shared/AnswerableListBuilder.cs
Normal file
26
Advent Of Code Library/Shared/AnswerableListBuilder.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace AdventOfCodeLibrary.Shared
|
||||||
|
{
|
||||||
|
public class AnswerableListBuilder
|
||||||
|
{
|
||||||
|
public static IEnumerable<IAnswerable> AnswerableList()
|
||||||
|
{
|
||||||
|
Type type = typeof(IAnswerable);
|
||||||
|
IEnumerable<IAnswerable?> instances =
|
||||||
|
Assembly.GetExecutingAssembly().GetTypes()
|
||||||
|
.Where(t =>
|
||||||
|
t.GetInterfaces().Contains(type) &&
|
||||||
|
t.GetConstructor(Type.EmptyTypes) != null
|
||||||
|
)
|
||||||
|
.Select(t => Activator.CreateInstance(t) as IAnswerable)
|
||||||
|
.Where(i => i != null);
|
||||||
|
|
||||||
|
// try to filter null
|
||||||
|
var noneNull = instances.Where(i => i != null);
|
||||||
|
|
||||||
|
|
||||||
|
return new List<IAnswerable>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Advent Of Code Library/Shared/ByteHelper.cs
Normal file
11
Advent Of Code Library/Shared/ByteHelper.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
namespace AdventOfCodeLibrary.Shared
|
||||||
|
{
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
public class ByteHelper
|
||||||
|
{
|
||||||
|
public static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes);
|
||||||
|
|
||||||
|
public static string[] GetAsStringArray(byte[] bytes) => Encoding.UTF8.GetString(bytes).Split(Environment.NewLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
Advent Of Code Library/Shared/IAnswerable.cs
Normal file
20
Advent Of Code Library/Shared/IAnswerable.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
namespace AdventOfCodeLibrary.Shared
|
||||||
|
{
|
||||||
|
public interface IAnswerable
|
||||||
|
{
|
||||||
|
int Year { get; set; }
|
||||||
|
|
||||||
|
int Day { get; set; }
|
||||||
|
|
||||||
|
int Part { get; set; }
|
||||||
|
|
||||||
|
string DefaultInputFile {
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return $"../../../../Advent Of Code Library/{Year}/Day {Day:00}/day-{Day:00}-input.txt";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string GetAwner(byte[] data);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user