-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBingoSubsystem.java
More file actions
192 lines (178 loc) · 6.31 KB
/
BingoSubsystem.java
File metadata and controls
192 lines (178 loc) · 6.31 KB
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Solution to Day 4: Giant Squid.
*
* @author bluebillxp
*/
public class BingoSubsystem {
private static int ROW_SIZE = 5;
private static int COL_SIZE = 5;
private static class Cell {
int value;
int row;
int column;
boolean check;
}
private static class BingoBoard {
Cell[][] cells = new Cell[ROW_SIZE][COL_SIZE];
HashMap<Integer, Cell> index = new HashMap<>(ROW_SIZE * COL_SIZE);
boolean won;
}
public static void main(String[] args) {
List<String> input = AdventHelper.readInput("input-day4-giant-squid.txt");
List<BingoBoard> boards = buildBoards(input);
String[] randomNumbers = input.get(0).split(",");
System.out.println("Day 4: Giant Squid: " + boards.size() + " boards loaded.");
System.out.println("Day 4: Giant Squid --- Part One ---");
System.out.println("What will your final score be if you choose that board?");
final int answerOne = solutionPartOne(boards, randomNumbers);
System.out.println("Answer: " + answerOne);
System.out.println("\nDay 4: Giant Squid --- Part Two ---");
System.out.println("Once it wins, what would its final score be?");
final int answerTwo = solutionPartTwo(boards, randomNumbers);
System.out.println("Answer: " + answerTwo);
}
/**
* Calculates the final score of the winning board.
*
* @param boards Defined bingo boards from the challenge.
* @param randomNumbers Defined random numbers from the challenge.
*
* @return answer
*/
private static int solutionPartOne(List<BingoBoard> boards, String[] randomNumbers) {
int lastCalledNum = 0;
BingoBoard winningBoard = null;
for (String num : randomNumbers) {
Integer value = Integer.valueOf(num);
winningBoard = findWinningBoard(boards, value);
if (winningBoard != null) {
lastCalledNum = value;
break;
}
}
return computeBoardScore(winningBoard, lastCalledNum);
}
/**
* Calculates the final score of the least winning board.
*
* @param boards Defined bingo boards from the challenge.
* @param randomNumbers Defined random numbers from the challenge.
*
* @return answer
*/
private static int solutionPartTwo(List<BingoBoard> boards, String[] randomNumbers) {
int lastCalledNum = 0;
BingoBoard leastWinningBoard = null;
for (String num : randomNumbers) {
Integer value = Integer.valueOf(num);
leastWinningBoard = findLeastWinningBoard(boards, value);
if (leastWinningBoard != null) {
lastCalledNum = value;
break;
}
}
return computeBoardScore(leastWinningBoard, lastCalledNum);
}
private static int computeBoardScore(BingoBoard board, int lastCalledNum) {
int unmarkedSum = 0;
for (Cell cell : board.index.values()) {
if (!cell.check) {
unmarkedSum += cell.value;
}
}
return unmarkedSum * lastCalledNum;
}
private static BingoBoard findWinningBoard(List<BingoBoard> boards, Integer num) {
BingoBoard winningBoard = null;
for (BingoBoard board : boards) {
Cell cell = board.index.get(num);
if (cell != null) {
cell.check = true;
int cntRow = 0;
int cntColumn = 0;
for (int i = 0; i < ROW_SIZE; i++) {
if (board.cells[cell.row][i].check) {
cntRow++;
}
if (board.cells[i][cell.column].check) {
cntColumn++;
}
}
if (cntRow == ROW_SIZE || cntColumn == COL_SIZE) {
board.won = true;
winningBoard = board;
break;
}
}
}
return winningBoard;
}
private static BingoBoard findLeastWinningBoard(List<BingoBoard> boards, Integer num) {
BingoBoard leastWinningBoard = null;
int cntWinningBoards = 0;
for (BingoBoard board : boards) {
if (board.won) {
cntWinningBoards++;
continue;
}
Cell cell = board.index.get(num);
if (cell != null) {
cell.check = true;
int cntRow = 0;
int cntColumn = 0;
for (int i = 0; i < ROW_SIZE; i++) {
if (board.cells[cell.row][i].check) {
cntRow++;
}
if (board.cells[i][cell.column].check) {
cntColumn++;
}
}
if (cntRow == ROW_SIZE || cntColumn == COL_SIZE) {
board.won = true;
cntWinningBoards++;
leastWinningBoard = board;
}
}
}
if (cntWinningBoards == boards.size()) {
return leastWinningBoard;
}
return null;
}
private static List<BingoBoard> buildBoards(List<String> input) {
List<BingoBoard> boards = new ArrayList<>();
BingoBoard board = new BingoBoard();
int row = 0;
for (int i = 1; i < input.size(); i++) {
if (row == ROW_SIZE) {
row = 0;
boards.add(board);
board = new BingoBoard();
}
String line = input.get(i);
if (line.isEmpty()) {
continue;
}
String[] values = line.split(" ");
int column = 0;
for (int j = 0; j < values.length; j++) {
if (values[j].isEmpty()) {
continue;
}
Cell cell = new Cell();
cell.value = Integer.valueOf(values[j]);
cell.row = row;
cell.column = column;
board.cells[row][column] = cell;
board.index.put(cell.value, cell);
column++;
}
row++;
}
return boards;
}
}