From 0fbc63731708a34189ec3a4cad6d1a45b0198588 Mon Sep 17 00:00:00 2001 From: Rob Date: Thu, 7 Dec 2023 23:57:00 +0100 Subject: [PATCH] Small code improvements --- AdventOfCode.Solutions/2023/Day 07/Day07.cs | 55 +++++++++------------ 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/AdventOfCode.Solutions/2023/Day 07/Day07.cs b/AdventOfCode.Solutions/2023/Day 07/Day07.cs index 1371360..53895c3 100644 --- a/AdventOfCode.Solutions/2023/Day 07/Day07.cs +++ b/AdventOfCode.Solutions/2023/Day 07/Day07.cs @@ -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 CardValues = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']; private static readonly List 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 GetSolutionPart1() + { + return (await _inputReader.ReadAsArrayString()) + .Select(l => new Hand(l)) + .Order() + .Select((h, i) => h.Bid * (i + 1)) + .Sum() + .ToString(); + } + + public async Task 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 { 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 GetSolutionPart1() - { - List 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 GetSolutionPart2() - { - List 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(); - } + } } \ No newline at end of file