-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartTwo.java
61 lines (48 loc) · 1.89 KB
/
PartTwo.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* --- Day 2: Cube Conundrum --- --- Part Two ---
*
*
* https://adventofcode.com/2023/day/2
**/
public class PartTwo {
public static void main(String[] args) throws Exception {
int totalMinCubes = 0;
try {
Scanner scanner = new Scanner(new File("./src/input.txt"));
while (scanner.hasNextLine()) {
String input = scanner.nextLine();
int getIndexOf = input.indexOf(":");
input = input.substring(getIndexOf + 1);
String[] splitBySet = input.split(";");
int minRed = 0;
int minGreen = 0;
int minBlue = 0;
for (int i = 0; i < splitBySet.length; i++) {
String[] splitByComma = splitBySet[i].split(",");
for (int j = 0; j < splitByComma.length; j++) {
String current = splitByComma[j].trim();
String numOfCubes = current.substring(0,
current.indexOf(" "));
int num = Integer.parseInt(numOfCubes);
if (current.contains("red") && num > minRed) {
minRed = num;
} else if (current.contains("green") && num > minGreen) {
minGreen = num;
} else if (current.contains("blue") && num > minBlue) {
minBlue = num;
}
}
}
int powerOfSet = minRed * minGreen * minBlue;
totalMinCubes += powerOfSet;
}
scanner.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
System.out.println(totalMinCubes);
}
}