-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay4.java
156 lines (148 loc) · 5.09 KB
/
Day4.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;
public class Day4 {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("day4.txt");
part1(file);
part2(file);
}
static void part2(File file) throws FileNotFoundException {
Scanner scan = new Scanner(file);
Map<int[][], Boolean[][]> boards = new HashMap<>();
String[] draws = scan.nextLine().split(",");
while (scan.hasNextLine()) {
int[][] nums = new int[5][5];
Boolean[][] vals = new Boolean[5][5];
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
nums[i][j] = scan.nextInt();
vals[i][j] = false;
}
}
boards.put(nums, vals);
}
scan.close();
int lastDraw = 0;
Map<int[][], Boolean[][]> lastBoard = new HashMap<>(boards);
for (int i=0; i<draws.length; i++) {
int draw = Integer.parseInt(draws[i]);
lastDraw = draw;
for (var board : boards.entrySet()) {
var nums = board.getKey();
Boolean[][] vals = board.getValue();
for (int j=0; j<5; j++) {
for (int k=0; k<5; k++) {
if (nums[j][k] == draw) {
vals[j][k] = true;
boards.put(nums, vals);
boolean bingo = checkBoard2(board, nums[j][k]);
if (bingo) {
lastBoard.remove(nums);
if (lastBoard.isEmpty()) {
finish(board, lastDraw);
System.exit(0);
}
}
}
}
}
}
}
}
static boolean checkBoard2(Entry<int[][], Boolean[][]> board, int n) {
boolean bingo = true;
var vals = board.getValue();
for (int i=0; i<5; i++) {
bingo = true;
for (int j=0; j<5; j++) {
if (Boolean.FALSE.equals(vals[i][j]))
bingo = false;
}
if (bingo) return true;
}
for (int i=0; i<5; i++) {
bingo = true;
for (int j=0; j<5; j++) {
if (Boolean.FALSE.equals(vals[j][i]))
bingo = false;
}
if (bingo) return true;
}
return false;
}
static void part1(File file) throws FileNotFoundException {
Scanner scan = new Scanner(file);
Map<int[][], Boolean[][]> boards = new HashMap<>();
String[] draws = scan.nextLine().split(",");
while (scan.hasNextLine()) {
int[][] nums = new int[5][5];
Boolean[][] vals = new Boolean[5][5];
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
nums[i][j] = scan.nextInt();
vals[i][j] = false;
}
}
boards.put(nums, vals);
}
scan.close();
for (int i=0; i<draws.length; i++) {
int draw = Integer.parseInt(draws[i]);
for (var board : boards.entrySet()) {
var nums = board.getKey();
Boolean[][] vals = board.getValue();
for (int j=0; j<5; j++) {
for (int k=0; k<5; k++) {
if (nums[j][k] == draw) {
vals[j][k] = true;
boards.put(nums, vals);
checkBoard1(board, nums[j][k]);
}
}
}
}
}
}
// 7 4 9 5 11
// 17 23 2 0 14 21
// 24: WINNER!
static void checkBoard1(Entry<int[][], Boolean[][]> board, int n) {
boolean bingo = true;
var vals = board.getValue();
for (int i=0; i<5; i++) {
bingo = true;
for (int j=0; j<5; j++) {
if (Boolean.FALSE.equals(vals[i][j]))
bingo = false;
}
if (bingo) finish(board, n);
}
for (int i=0; i<5; i++) {
bingo = true;
for (int j=0; j<5; j++) {
if (Boolean.FALSE.equals(vals[j][i]))
bingo = false;
}
if (bingo) finish(board, n);
}
}
static void finish(Entry<int[][], Boolean[][]> board, int n) {
int sum = 0;
var nums = board.getKey();
var vals = board.getValue();
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
if (Boolean.FALSE.equals(vals[i][j])) {
sum += nums[i][j];
}
}
}
// Part 1: 188 * 24 = 4512
// Part 2: 148 * 13 = 1924
System.out.format("%d*%d = %d\n", sum, n, n*sum);
}
}