-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle7.java
196 lines (174 loc) · 5.42 KB
/
Puzzle7.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package advent2023;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multiset;
import com.google.common.collect.TreeMultiset;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Supplier;
/**
* @author Éamonn McManus
*/
public class Puzzle7 {
public static void main(String[] args) throws Exception {
try (InputStream in = Puzzle7.class.getResourceAsStream("puzzle7.txt")) {
String lineString = new String(in.readAllBytes(), UTF_8);
List<String> lines = List.of(lineString.split("\n"));
List<Hand> hands = lines.stream().map(Puzzle7::parseHand).sorted().toList();
// Part 1
long sum = 0;
for (int i = 0; i < hands.size(); i++) {
sum += (i + 1) * hands.get(i).bid;
}
System.out.println("Sum: " + sum);
// Part 2
List<Hand> jokerHands = hands.stream().sorted(JOKER_HAND_COMPARATOR).toList();
long jokerSum = 0;
for (int i = 0; i < jokerHands.size(); i++) {
jokerSum += (i + 1) * jokerHands.get(i).bid;
}
System.out.println("Joker sum: " + jokerSum);
}
}
private static Hand parseHand(String line) {
String[] words = line.split(" ");
assert words.length == 2;
assert words[0].length() == 5;
int bid = Integer.parseInt(words[1]);
List<Card> cards = words[0].chars().mapToObj(c -> Card.of((char) c)).toList();
return new Hand(cards, bid);
}
static class Hand implements Comparable<Hand> {
final List<Card> cards;
final int bid;
final Supplier<Score> score = Suppliers.memoize(this::computeScore);
final Supplier<Score> jokerScore = Suppliers.memoize(this::jokerScore);
Hand(List<Card> cards, int bid) {
this.cards = cards;
this.bid = bid;
}
@Override
public int compareTo(Hand that) {
int cmp = this.score.get().compareTo(that.score.get());
if (cmp != 0) {
return cmp;
}
for (int i = 0; i < cards.size(); i++) {
cmp = this.cards.get(i).compareTo(that.cards.get(i));
if (cmp != 0) {
return cmp;
}
}
return 0;
}
private Score computeScore() {
Multiset<Card> cardSet = TreeMultiset.create(cards);
return scoreFor(cardSet);
}
private static Score scoreFor(Multiset<Card> cardSet) {
List<Integer> counts = cardSet.entrySet().stream().map(Multiset.Entry::getCount).toList();
if (counts.contains(5)) {
return Score.FIVE_OF_A_KIND;
} else if (counts.contains(4)) {
return Score.FOUR_OF_A_KIND;
} else if (counts.contains(3)) {
if (counts.contains(2)) {
return Score.FULL_HOUSE;
} else {
return Score.THREE_OF_A_KIND;
}
} else if (counts.contains(2)) {
if (counts.indexOf(2) != counts.lastIndexOf(2)) {
return Score.TWO_PAIR;
} else {
return Score.PAIR;
}
} else {
return Score.HIGH_CARD;
}
}
Score jokerScore() {
if (!cards.contains(Card.JACK)) {
return computeScore();
}
Multiset<Card> cardSet = TreeMultiset.create(cards);
int jokers = cardSet.count(Card.JACK);
cardSet.remove(Card.JACK, jokers);
if (cardSet.isEmpty()) {
return Score.FIVE_OF_A_KIND;
}
// Enumerating the cases is just a bit too tedious. It's always most beneficial to add the
// jokers to the most-frequent card. So just do that and see what the result is.
Multiset.Entry<Card> biggest =
cardSet.entrySet().stream().max(Comparator.comparing(Multiset.Entry::getCount)).get();
cardSet.add(biggest.getElement(), jokers);
return scoreFor(cardSet);
}
@Override
public String toString() {
return cards.stream().map(Object::toString).collect(joining(""));
}
}
enum Score {
HIGH_CARD,
PAIR,
TWO_PAIR,
THREE_OF_A_KIND,
FULL_HOUSE,
FOUR_OF_A_KIND,
FIVE_OF_A_KIND
}
private static final Comparator<Card> JOKER_COMPARATOR =
(a, b) -> {
int aOrd = a == Card.JACK ? -1 : a.ordinal();
int bOrd = b == Card.JACK ? -1 : b.ordinal();
return Integer.compare(aOrd, bOrd);
};
private static final Comparator<Hand> JOKER_HAND_COMPARATOR =
(a, b) -> {
int cmp = a.jokerScore.get().compareTo(b.jokerScore.get());
if (cmp != 0) {
return cmp;
}
for (int i = 0; i < a.cards.size(); i++) {
cmp = JOKER_COMPARATOR.compare(a.cards.get(i), b.cards.get(i));
if (cmp != 0) {
return cmp;
}
}
return cmp;
};
enum Card {
TWO("2"),
THREE("3"),
FOUR("4"),
FIVE("5"),
SIX("6"),
SEVEN("7"),
EIGHT("8"),
NINE("9"),
TEN("T"),
JACK("J"),
QUEEN("Q"),
KING("K"),
ACE("A");
private final String name;
Card(String name) {
this.name = name;
}
static Card of(char c) {
return CHAR_TO_CARD.get(c);
}
@Override
public String toString() {
return name;
}
private static final ImmutableMap<Character, Card> CHAR_TO_CARD =
Arrays.stream(values()).collect(toImmutableMap(card -> card.name.charAt(0), card -> card));
}
}