-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChallenge.java
More file actions
52 lines (43 loc) · 1.3 KB
/
Challenge.java
File metadata and controls
52 lines (43 loc) · 1.3 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Challenge {
private ArrayList<Integer> parsedInput;
public Challenge(String filename) throws FileNotFoundException {
parsedInput = new ArrayList<>();
File inputFile = new File(filename);
Scanner reader = new Scanner(inputFile);
int acc = 0;
while (reader.hasNextLine()) {
String thisLine = reader.nextLine();
if ("".equals(thisLine)) {
if (acc != 0) {
parsedInput.add(acc);
acc = 0;
}
} else {
acc += Integer.parseInt(thisLine);
}
}
reader.close();
}
public int partOne() {
int largest = 0;
for (Integer x : parsedInput) {
if (x > largest) {
largest = x;
}
}
return largest;
}
public int partTwo() {
ArrayList<Integer> clonedInput = (ArrayList<Integer>) parsedInput.clone();
clonedInput.sort(Integer::compareTo);
int acc = 0;
for (int i = clonedInput.size()-1; i >= clonedInput.size() - 3; i -= 1) {
acc += clonedInput.get(i);
}
return acc;
}
}