-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_7.cs
153 lines (133 loc) · 5.53 KB
/
Day_7.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
namespace AdventOfCode_2023
{
public class Day_7 : Day
{
private enum Strength
{
HighCard,
OnePair,
TwoPair,
ThreeOfAKind,
FullHouse,
FourOfAKind,
FiveOfAKind
}
private abstract class Hand : IComparable<Hand>
{
protected abstract List<char> CardOrder { get; set; }
public required string Cards { get; set; }
public long Bid { get; set; }
public abstract Strength Strength { get; }
public int CompareTo(Hand? other)
{
if (other == null)
return -1;
int res = this.Strength - other.Strength;
int i = 0;
while (res == 0 && i < Cards.Length)
{
res = CardOrder.IndexOf(Cards[i]) - CardOrder.IndexOf(other.Cards[i]);
i++;
}
return res;
}
}
private class FirstPartHand : Hand
{
protected override List<char> CardOrder { get; set; } = new List<char> { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
public override Strength Strength
{
get
{
List<char> distinctChars = Cards.ToCharArray().Distinct().ToList();
switch (distinctChars.Count)
{
case 1:
return Strength.FiveOfAKind;
case 2:
int firstCharOccurence = Cards.Count(c => c == distinctChars[0]);
if (firstCharOccurence == 4 || firstCharOccurence == 1)
return Strength.FourOfAKind;
else
return Strength.FullHouse;
case 3:
foreach (char dc in distinctChars)
{
if (Cards.Count(c => c == dc) == 3)
return Strength.ThreeOfAKind;
}
return Strength.TwoPair;
case 4: return Strength.OnePair;
default: return Strength.HighCard;
}
}
}
}
public override long FirstPart()
{
List<FirstPartHand> hands =
inputs.Select(input => {
string[] strings = input.Split(" ");
return new FirstPartHand { Cards = strings[0], Bid = long.Parse(strings[1]) };
}).ToList();
hands.Sort();
return hands.Select((hand, index) => hand.Bid * (index + 1)).Sum();
}
private class SecondPartHand : Hand
{
protected override List<char> CardOrder { get; set; } = new List<char> { 'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A' };
public override Strength Strength
{
get
{
List<char> distinctCharsWithoutJokers = Cards.ToCharArray().Where(c => c != 'J').Distinct().ToList();
int jokers = Cards.Count(c => c == 'J');
switch (distinctCharsWithoutJokers.Count)
{
case 0:
case 1:
return Strength.FiveOfAKind;
case 2:
if (jokers > 1)
{
return Strength.FourOfAKind;
}
int firstCharOccurence = Cards.Count(c => c == distinctCharsWithoutJokers[0]);
if (jokers == 0)
{
return (firstCharOccurence == 2 || firstCharOccurence == 3) ? Strength.FullHouse : Strength.FourOfAKind;
}
else
{
return (firstCharOccurence == 2) ? Strength.FullHouse : Strength.FourOfAKind;
}
case 3:
if (jokers > 0)
{
return Strength.ThreeOfAKind;
}
else
{
int maxOccurence = 0;
foreach (char dc in distinctCharsWithoutJokers)
maxOccurence = Math.Max(maxOccurence, Cards.Count(c => c == dc));
return maxOccurence == 3 ? Strength.ThreeOfAKind : Strength.TwoPair;
}
case 4: return Strength.OnePair;
default: return Strength.HighCard;
}
}
}
}
public override long SecondPart()
{
List<SecondPartHand> hands =
inputs.Select(input => {
string[] strings = input.Split(" ");
return new SecondPartHand { Cards = strings[0], Bid = long.Parse(strings[1]) };
}).ToList();
hands.Sort();
return hands.Select((hand, index) => hand.Bid * (index + 1)).Sum();
}
}
}