-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023_07.cs
156 lines (128 loc) · 3.8 KB
/
2023_07.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
154
155
156
namespace AOC.CSharp;
public static class AOC2023_07
{
public static long Solve1(string[] lines)
{
return Solve(lines, false);
}
public static long Solve2(string[] lines)
{
return Solve(lines, true);
}
private static long Solve(string[] lines, bool joker)
{
var hands = lines.Select(x => Parse(x, joker)).ToList();
var strengths = hands.Select(x => GetStrength(x, joker)).ToList();
var ordered = strengths.Order(new StrengthComparer()).ToList();
long sum = 0;
for (int i = 0; i < ordered.Count; i++)
{
Strength s = ordered[i];
int rank = i + 1;
sum += rank * s.Hand.Bid;
}
return sum;
}
private static Strength GetStrength(Hand h, bool joker)
{
List<int[]> cardArrangements = new();
if (joker)
{
for (int i = 2; i <= 14; i++)
{
// Try substituting each other card for each joker
cardArrangements.Add(h.Cards.Select(x => x == 1 ? i : x).ToArray());
}
}
else
{
cardArrangements.Add(h.Cards);
}
int bestType = 1;
foreach (int[] ca in cardArrangements)
{
Dictionary<int, int> counts = ca.GroupBy(x => x)
.ToDictionary(x2 => x2.Key, x2 => x2.Count());
if (counts.Values.Any(c => c == 5))
{
return new(7, h);
}
if (bestType < 6 && counts.Values.Any(c => c == 4))
{
bestType = Math.Max(bestType, 6);
}
if (bestType < 5 && counts.Values.Any(c => c == 3) && counts.Values.Any(c => c == 2))
{
bestType = Math.Max(bestType, 5);
}
if (bestType < 4 && counts.Values.Any(c => c == 3))
{
bestType = Math.Max(bestType, 4);
}
if (bestType < 3 && counts.Values.Count(c => c == 2) == 2)
{
bestType = Math.Max(bestType, 3);
}
if (bestType < 2 && counts.Values.Any(c => c == 2))
{
bestType = Math.Max(bestType, 2);
}
}
return new(bestType, h);
}
private static Hand Parse(string line, bool joker)
{
string[] splits = line.Split(" ");
string cards = splits[0];
int bid = int.Parse(splits[1]);
int ToNum(char ch)
{
return ch switch
{
'T' => 10,
'J' when !joker => 11,
'J' when joker => 1,
'Q' => 12,
'K' => 13,
'A' => 14,
_ => int.Parse(ch.ToString())
};
}
int[] cardsAsNums = cards.Select(ToNum).ToArray();
return new(cardsAsNums, bid);
}
private class StrengthComparer : IComparer<Strength>
{
public int Compare(Strength x, Strength y)
{
if (x.Type < y.Type)
{
return -1;
}
if (x.Type > y.Type)
{
return 1;
}
for (int i = 0; i < x.Hand.Cards.Length; i++)
{
int cardX = x.Hand.Cards[i];
int cardY = y.Hand.Cards[i];
if (cardX < cardY)
{
return -1;
}
if (cardX > cardY)
{
return 1;
}
}
return 0;
}
}
private record Strength(int Type, Hand Hand)
{
public override string ToString() =>
$"{Type} | {string.Join(", ", Hand.Cards)} | {Hand.Bid}";
}
private record Hand(int[] Cards, int Bid);
}