-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay01.java
More file actions
59 lines (54 loc) · 1.22 KB
/
Day01.java
File metadata and controls
59 lines (54 loc) · 1.22 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
import java.util.List;
import java.util.ArrayList;
class Day01{
public static double p1(List<String> input){
int elf=0;
int max=0;
for(String line: input){
if (!line.isEmpty()){
elf+= Integer.parseInt(line);
}
else{
if(elf > max){
max=elf;
}
elf=0;
}
}
if (elf>max){
max=elf;
}
return max;
}
public static double p2(List<String> input){
int elf=0;
ArrayList<Integer> topElves = new ArrayList<Integer>();
for(String line: input){
if (!line.isEmpty()){
elf+= Integer.parseInt(line);
}
else{
updateLeaderboard(topElves,elf,3);
elf=0;
}
}
updateLeaderboard(topElves,elf,3);
int leaderTotal=0;
for(int leader: topElves){
leaderTotal += leader;
}
return leaderTotal;
}
public static void updateLeaderboard(ArrayList<Integer> leaders, int newScore, int leaderboardLength){
for (int i=0;i<leaderboardLength;i++){
if ((i >= leaders.size()) || (newScore>leaders.get(i))){
leaders.add(i,newScore);
break;
}
}
while (leaders.size() > leaderboardLength)
{
leaders.remove(leaders.size()-1);
}
}
}