-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD4.java
203 lines (186 loc) · 8.24 KB
/
D4.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
197
198
199
200
201
202
203
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class D4 {
private static BufferedReader dataReader() {
File file = new File("pathToYourPuzzle");
try {
BufferedReader data = new BufferedReader(new FileReader(file));
return data;
} catch (FileNotFoundException e) {
System.out.println("The file does not exist.");
return null;
}
}
private static HashMap<String, Integer> score(ArrayList<Integer> drawnNumbers, ArrayList<ArrayList<Integer>> currentBingoCard) {
HashMap<String, Integer> result = new HashMap<>();
int bingoCardScore = 0;
int idx = 0;
int smallerIdx = 0;
int minimusBingoNumbers = 5;
while (idx < drawnNumbers.size()) {
// first, drawing numbers to match the minimus numbers for bingo requirement
while (idx < minimusBingoNumbers) {
currentBingoCard = updateBingoCard(drawnNumbers.get(idx), currentBingoCard);
idx += 1;
}
boolean bingo = bingoCheck(currentBingoCard);
// if bingo, calculate the bingoCardScore
if (bingo) {
bingoCardScore = calculateScore(drawnNumbers.get(idx-1), currentBingoCard);
smallerIdx = idx -1;
break;
} else {
// if no bingo happens, keep drawing a new number
currentBingoCard = updateBingoCard(drawnNumbers.get(idx), currentBingoCard);
}
idx += 1;
}
result.put("drawnIdx", smallerIdx);
result.put("bingoCardScore", bingoCardScore);
return result;
}
private static ArrayList<ArrayList<Integer>> updateBingoCard(Integer drawnNum, ArrayList<ArrayList<Integer>> currentBingoCard) {
boolean found = false;
for (ArrayList<Integer> numList : currentBingoCard) {
if (!found) {
for (int i = 0; i < numList.size(); i++) {
if (numList.get(i).equals(drawnNum)) {
numList.set(i, -1);
found = true;
}
}
}
}
return currentBingoCard;
}
private static boolean bingoCheck(ArrayList<ArrayList<Integer>> currentBingoCard) {
HashMap<Integer, Boolean> columnsToCheck = new HashMap<>();
for (int r = 0; r < currentBingoCard.size(); r++) {
int bingoRow = 0;
for (int c = 0; c < currentBingoCard.size(); c++) {
// check rows
if (currentBingoCard.get(r).get(c) == -1){
bingoRow += 1;
if (r == 0) columnsToCheck.put(c, true);
}
// check columns
if ((r != 0) && (!columnsToCheck.containsKey(c) || currentBingoCard.get(r).get(c) != -1)) columnsToCheck.remove(c);
}
if (bingoRow == 5) return true;
bingoRow = 0;
}
if (!columnsToCheck.isEmpty()) return true;
return false;
}
private static int calculateScore(Integer drawnNumber, ArrayList<ArrayList<Integer>> currentBingoCard) {
int total = 0;
for (int r = 0; r < currentBingoCard.size(); r++) {
for (int c = 0; c < currentBingoCard.size(); c++) {
// check rows
if (!(currentBingoCard.get(r).get(c) == -1)) {
total += currentBingoCard.get(r).get(c);
}
}
}
return total * drawnNumber;
}
private static void part1() throws IOException {
BufferedReader inputData = dataReader();
String depth;
ArrayList<Integer> drawnNumbers = new ArrayList<>();
int numLine = 0;
int bingoBoardSize = 0;
int currentScoreForMinDrawnIdx = 0;
int currentMinIdx = -1;
ArrayList<ArrayList<Integer>> currentBingoCard = new ArrayList<>();
while ((depth = inputData.readLine()) != null) {
// collect the 1st line for drawn numbers
if (numLine == 0) {
for (String num : depth.split(",")) {
drawnNumbers.add(Integer.parseInt(num));
}
numLine += 1;
} else {
// collect five lines for each bingo card
if (!depth.isEmpty() && bingoBoardSize < 5) {
// convert string[] to integer[]
String[] stringDepth = depth.split(" ");
List<String> stringDepthList = new ArrayList<String>(Arrays.asList(stringDepth));
stringDepthList.removeAll(Arrays.asList("", null));
ArrayList<Integer> numDepthList = new ArrayList<>();
for (String num : stringDepthList) {
numDepthList.add(Integer.parseInt(num));
}
currentBingoCard.add(bingoBoardSize, numDepthList);
bingoBoardSize += 1;
} else if (depth.isEmpty() && bingoBoardSize == 5) {
// reset for the next bingo card
currentBingoCard .clear();
bingoBoardSize = 0;
}
// calculate each bingo card
if (bingoBoardSize == 5) {
HashMap<String, Integer> currentResult = score(drawnNumbers, currentBingoCard);
if (currentMinIdx == -1 || currentMinIdx > currentResult.get("drawnIdx")) {
currentMinIdx = currentResult.get("drawnIdx");
currentScoreForMinDrawnIdx = currentResult.get("bingoCardScore");
}
}
}
}
System.out.println(String.format("part 1: %s", currentScoreForMinDrawnIdx));
}
private static void part2() throws IOException {
BufferedReader inputData = dataReader();
String depth;
ArrayList<Integer> drawnNumbers = new ArrayList<>();
int numLine = 0;
int bingoBoardSize = 0;
int currentScoreForMinDrawnIdx = 0;
int currentMinIdx = -1;
ArrayList<ArrayList<Integer>> currentBingoCard = new ArrayList<>();
while ((depth = inputData.readLine()) != null) {
// collect the 1st line for drawn numbers
if (numLine == 0) {
for (String num : depth.split(",")) {
drawnNumbers.add(Integer.parseInt(num));
}
numLine += 1;
} else {
// collect five lines for each bingo card
if (!depth.isEmpty() && bingoBoardSize < 5) {
// convert string[] to integer[]
String[] stringDepth = depth.split(" ");
List<String> stringDepthList = new ArrayList<String>(Arrays.asList(stringDepth));
stringDepthList.removeAll(Arrays.asList("", null));
ArrayList<Integer> numDepthList = new ArrayList<>();
for (String num : stringDepthList) {
numDepthList.add(Integer.parseInt(num));
}
currentBingoCard.add(bingoBoardSize, numDepthList);
bingoBoardSize += 1;
} else if (depth.isEmpty() && bingoBoardSize == 5) {
// reset for the next bingo card
currentBingoCard .clear();
bingoBoardSize = 0;
}
// calculate each bingo card
if (bingoBoardSize == 5) {
HashMap<String, Integer> currentResult = score(drawnNumbers, currentBingoCard);
if (currentMinIdx == -1 || currentMinIdx < currentResult.get("drawnIdx")) {
currentMinIdx = currentResult.get("drawnIdx");
currentScoreForMinDrawnIdx = currentResult.get("bingoCardScore");
}
}
}
}
System.out.println(String.format("part 2: %s", currentScoreForMinDrawnIdx));
}
public static void main(String[] args) throws Exception {
part1();
part2();
}
}