summaryrefslogtreecommitdiff
path: root/day07/day07.java
blob: 6688f5e0d66ce16c30c6e85fb6796dd54335b66b (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class day07 {
  public static void main(String args[]) throws IOException {
    RandomAccessFile input = new RandomAccessFile("input", "r");
    System.out.println("Day 07 Part 1: " + part1(input));
    input.seek(0);
    System.out.println("Day 07 Part 2: " + part2(input));
    input.close();
  }

  public static String part1(RandomAccessFile input) throws IOException {
    Game game = new Game();

    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);
  }

  public static String part2(RandomAccessFile input) throws IOException {
    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) {
    String[] linea = line.split(" +");
    this.hands.add(
      new Hand(
        linea[0].toCharArray(),
        Long.parseLong(linea[1]),
        this.wild
      )
    );
  }

  public void rank() {
    Collections.sort(hands);
  }
}

class Hand implements Comparable<Hand> {
  int[] cards;
  long bid;
  boolean wild;

  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]) {
        case '2':
          this.cards[i] = 2;
          break;
        case '3':
          this.cards[i] = 3;
          break;
        case '4':
          this.cards[i] = 4;
          break;
        case '5':
          this.cards[i] = 5;
          break;
        case '6':
          this.cards[i] = 6;
          break;
        case '7':
          this.cards[i] = 7;
          break;
        case '8':
          this.cards[i] = 8;
          break;
        case '9':
          this.cards[i] = 9;
          break;
        case 'T':
          this.cards[i] = 10;
          break;
        case 'J':
          this.cards[i] = this.wild ? 1 : 11;
          break;
        case 'Q':
          this.cards[i] = 12;
          break;
        case 'K':
          this.cards[i] = 13;
          break;
        case 'A':
          this.cards[i] = 14;
          break;
      }
    }
    this.bid = bid;
  }

  public int compareTo(Hand other) {
    if (this.type() > other.type()) {
      return 1;
    } else if (this.type() < other.type()) {
      return -1;
    }
    for (int i = 0; i < 5; i++) {
      if (this.cards[i] > other.cards[i]) {
        return 1;
      } else if (this.cards[i] < other.cards[i]) {
        return -1;
      }
    }
    return 0;
  }

  private int type() {
    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;
    } else if (
        s[0] == s[1] && s[1] == s[2] && s[2] == s[3] ||
        s[1] == s[2] && s[2] == s[3] && s[3] == s[4] ) {
      return 5;
    } else if (
        s[0] == s[1]    &&    s[2] == s[3] && s[3] == s[4] ||
        s[0] == s[1] && s[1] == s[2]    &&    s[3] == s[4] ) {
      return 4;
    } else if (
        s[0] == s[1] && s[1] == s[2] ||
        s[1] == s[2] && s[2] == s[3] ||
        s[2] == s[3] && s[3] == s[4] ) {
      return 3;
    } else if (
        s[0] == s[1] && s[2] == s[3] ||
        s[0] == s[1] && s[3] == s[4] ||
        s[1] == s[2] && s[3] == s[4] ) {
      return 2;
    } else if (
        s[0] == s[1] ||
        s[1] == s[2] ||
        s[2] == s[3] ||
        s[3] == s[4] ){
      return 1;
    } else {
      return 0;
    }
  }
}