Small code improvements

This commit is contained in:
Rob 2023-12-07 23:57:00 +01:00
parent a6113210aa
commit 0fbc637317

View File

@ -4,12 +4,32 @@ namespace AdventOfCode.Solutions._2023
{
public class Day07(InputReader reader) : IChallange
{
private InputReader _inputReader = reader;
private readonly InputReader _inputReader = reader;
private static readonly List<char> CardValues = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'];
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 }
public async Task<string> GetSolutionPart1()
{
return (await _inputReader.ReadAsArrayString())
.Select(l => new Hand(l))
.Order()
.Select((h, i) => h.Bid * (i + 1))
.Sum()
.ToString();
}
public async Task<string> GetSolutionPart2()
{
return (await _inputReader.ReadAsArrayString())
.Select(l => new Hand(l, true))
.Order()
.Select((h, i) => h.Bid * (i + 1))
.Sum()
.ToString();
}
private record Hand : IComparable<Hand>
{
public int Bid { get; set; }
@ -38,7 +58,6 @@ namespace AdventOfCode.Solutions._2023
char toReplace = finalTestLine.Where(c => c != 'J').GroupBy(c => c).OrderByDescending(g => g.Count()).First().Key;
finalTestLine = finalTestLine.Replace('J', toReplace);
JokerSource = finalTestLine;
Console.WriteLine($"{Source} > {JokerSource}");
}
}
@ -102,36 +121,6 @@ namespace AdventOfCode.Solutions._2023
}
}
public async Task<string> GetSolutionPart1()
{
List<Hand> hands =
[
.. (await _inputReader.ReadAsArrayString()).Select(l => new Hand(l)).Order()
];
int sum = 0;
for (int index = 0; index < hands.Count; index++)
{
sum += hands[index].Bid * (index + 1);
}
return sum.ToString();
}
public async Task<string> GetSolutionPart2()
{
List<Hand> hands =
[
.. (await _inputReader.ReadAsArrayString()).Select(l => new Hand(l, true)).Order()
];
int sum = 0;
for (int index = 0; index < hands.Count; index++)
{
sum += hands[index].Bid * (index + 1);
}
return sum.ToString();
}
}
}