summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2023-12-07 16:56:23 +0000
committerNat Lasseter <user@4574.co.uk>2023-12-07 16:56:23 +0000
commit382cb297ae29a8c453740d1ad3236cab74b88182 (patch)
treee6148b03fcb763880199cdb4e77ba94aaffd2bf4
parent46550e1e79ada56e8c80c289f1103b9d85455593 (diff)
Day 07 part 2
-rw-r--r--day07/day07.java73
1 files changed, 68 insertions, 5 deletions
diff --git a/day07/day07.java b/day07/day07.java
index 3f4ae88..6688f5e 100644
--- a/day07/day07.java
+++ b/day07/day07.java
@@ -32,15 +32,35 @@ public class day07 {
}
public static String part2(RandomAccessFile input) throws IOException {
- return "WIP";
+ Game game = new Game(true);
+
+ String line;
+ while((line = input.readLine()) != null) {
+ game.parseHand(line);
+ }
+
+ game.rank();
+
+ long winnings = 0;
+ for (int i = 0; i < game.hands.size(); i++) {
+ winnings += ((i+1) * game.hands.get(i).bid);
+ }
+
+ return Long.toString(winnings);
}
}
class Game {
ArrayList<Hand> hands;
+ boolean wild;
public Game() {
+ this(false);
+ }
+
+ public Game(boolean wild) {
this.hands = new ArrayList<>();
+ this.wild = wild;
}
public void parseHand(String line) {
@@ -48,7 +68,8 @@ class Game {
this.hands.add(
new Hand(
linea[0].toCharArray(),
- Long.parseLong(linea[1])
+ Long.parseLong(linea[1]),
+ this.wild
)
);
}
@@ -61,8 +82,10 @@ class Game {
class Hand implements Comparable<Hand> {
int[] cards;
long bid;
+ boolean wild;
- public Hand(char[] cards, long bid) {
+ public Hand(char[] cards, long bid, boolean wild) {
+ this.wild = wild;
this.cards = new int[5];
for(int i = 0; i < 5; i++) {
switch (cards[i]) {
@@ -94,7 +117,7 @@ class Hand implements Comparable<Hand> {
this.cards[i] = 10;
break;
case 'J':
- this.cards[i] = 11;
+ this.cards[i] = this.wild ? 1 : 11;
break;
case 'Q':
this.cards[i] = 12;
@@ -130,6 +153,45 @@ class Hand implements Comparable<Hand> {
int[] s = this.cards.clone();
Arrays.sort(s);
+ if (this.wild) {
+ if (s[0] == 1 && s[1] == 1 && s[2] == 1 && s[3] == 1) { //4 or 5 jokers
+ return 6; //5 of a kind
+ } else if (s[0] == 1 && s[1] == 1 && s[2] == 1) { //3 jokers
+ if (s[3] == s[4]) { //pair
+ return 6; //5oak
+ } else { //high
+ return 5; //4oak
+ }
+ } else if (s[0] == 1 && s[1] == 1) { //2 jokers
+ if (s[2] == s[3] && s[3] == s[4]) {//3oak
+ return 6; //5oak
+ } else if (s[2] == s[3] || s[3] == s[4]) { //pair
+ return 5; //4oak
+ } else { //high
+ return 3; //3oak
+ }
+ } else if (s[0] == 1) { //1 jokers
+ if (
+ s[1] == s[2] && s[2] == s[3] && s[3] == s[4] ) { //4oak
+ return 6; //5oak
+ } else if (
+ s[1] == s[2] && s[2] == s[3] ||
+ s[2] == s[3] && s[3] == s[4] ) { //3oak
+ return 5; //4oak
+ } else if (s[1] == s[2] && s[3] == s[4]) { //2pair
+ return 4; //full
+ } else if (
+ s[1] == s[2] ||
+ s[2] == s[3] ||
+ s[3] == s[4] ) { //pair
+ return 3; //3oak
+ } else { //high
+ return 1;
+ }
+ }
+ }
+
+ //0 jokers
if (
s[0] == s[1] && s[1] == s[2] && s[2] == s[3] && s[3] == s[4] ) {
return 6;
@@ -157,7 +219,8 @@ class Hand implements Comparable<Hand> {
s[2] == s[3] ||
s[3] == s[4] ){
return 1;
+ } else {
+ return 0;
}
- return 0;
}
}