-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1Solution.java
More file actions
56 lines (45 loc) · 1.25 KB
/
Day1Solution.java
File metadata and controls
56 lines (45 loc) · 1.25 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class Day1 {
public static void main(String[] args) throws Exception{
String calories = """
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
""";
BufferedReader br = new BufferedReader(new FileReader("day1.txt"));
int calorie = 0;
int mostCalories = 0;
List<Integer> calorieList = new ArrayList<>();
String line;
//for (String line : calories.split("\n")) {
while ((line = br.readLine()) != null) {
String strCal = line.trim();
if (strCal.isBlank()) {
if (mostCalories < calorie) {
mostCalories = calorie;
}
calorieList.add(calorie);
calorie = 0;
continue;
}
calorie += Integer.parseInt(strCal);
}
calorieList.add(calorie);
if (mostCalories < calorie) {
mostCalories = calorie;
}
System.out.println(mostCalories);
calorieList.sort((a, b) -> b - a);
System.out.println(calorieList.get(0) + calorieList.get(1) + calorieList.get(2));
}
}