Skip to content

Commit d9a51e1

Browse files
Refactor code and improve encapsulation
The duplication in ranking calculation has been resolved by extracting it into a new method, ComputeFinalScore. This makes the PartOne and PartTwo methods clearer and shorter. Furthermore, the properties of the Hand and Card classes are now init-only, improving their immutability and encapsulation. Also made minor changes to method parameters and removed unused properties.
1 parent fc97122 commit d9a51e1

File tree

1 file changed

+12
-37
lines changed

1 file changed

+12
-37
lines changed

2023/Day07/Solution.cs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,7 @@ public object PartOne(string input)
1515
hand.Type = GetHandType(hand.Cards);
1616
}
1717

18-
var handsOrdered = hands.GroupBy(h => h.Type)
19-
.OrderBy(t => (int)t.Key)
20-
.ToList();
21-
22-
var rank = 1;
23-
var result = 0;
24-
foreach (var orderHand in handsOrdered)
25-
{
26-
if (orderHand.Count() == 1)
27-
{
28-
result += orderHand.First().Bid * rank;
29-
rank++;
30-
}
31-
else
32-
{
33-
// sort all hands by first card of each hand
34-
var sortedHands = orderHand.OrderBy(h => h.Cards.First().Strength).ToList();
35-
sortedHands.Sort(new HandComparer());
36-
37-
// Display sorted hands
38-
foreach (var hand in sortedHands)
39-
{
40-
result += hand.Bid * rank;
41-
rank++;
42-
}
43-
}
44-
}
45-
46-
return result;
18+
return ComputeFinalScore(hands);
4719
}
4820

4921
public object PartTwo(string input)
@@ -55,6 +27,11 @@ public object PartTwo(string input)
5527
hand.Type = GetHandType(hand.Cards, isPart2: true);
5628
}
5729

30+
return ComputeFinalScore(hands);
31+
}
32+
33+
private static int ComputeFinalScore(IEnumerable<Hand> hands)
34+
{
5835
var handsOrdered = hands.GroupBy(h => h.Type)
5936
.OrderBy(t => (int)t.Key)
6037
.ToList();
@@ -102,8 +79,7 @@ private static List<Hand> ParseInput(string input, bool isPart2 = false)
10279
cards.Add(new Card
10380
{
10481
Name = card.ToString(),
105-
Strength = isPart2 ? GetCardStrengthPart2(card) : GetCardStrength(card),
106-
Index = i
82+
Strength = isPart2 ? GetCardStrengthPart2(card) : GetCardStrength(card)
10783
});
10884
}
10985

@@ -143,7 +119,7 @@ private static int GetCardStrengthPart2(char card)
143119
};
144120
}
145121

146-
private static Type GetHandType(List<Card> cards, bool isPart2 = false)
122+
private static Type GetHandType(IReadOnlyCollection<Card> cards, bool isPart2 = false)
147123
{
148124
var cardGroups = cards.GroupBy(card => card.Name).ToList();
149125
var cardGroupCount = cardGroups.Count;
@@ -183,8 +159,8 @@ 3 when cardGroups.Any(group => group.Count() == 3) => Type.ThreeOfAKind,
183159

184160
public class Hand
185161
{
186-
public List<Card> Cards { get; set; }
187-
public int Bid { get; set; }
162+
public List<Card> Cards { get; init; }
163+
public int Bid { get; init; }
188164
public Type Type { get; set; }
189165
}
190166

@@ -201,9 +177,8 @@ public enum Type
201177

202178
public class Card
203179
{
204-
public string Name { get; set; }
205-
public int Strength { get; set; }
206-
public int Index { get; set; }
180+
public string Name { get; init; }
181+
public int Strength { get; init; }
207182
}
208183

209184
public class HandComparer : IComparer<Hand>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy