Add the solution for part 1 in Java
This commit is contained in:
parent
d4abca0859
commit
ebd77b57d4
1 changed files with 47 additions and 0 deletions
47
day_01/java/part_1.java
Normal file
47
day_01/java/part_1.java
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
class Part1 {
|
||||||
|
public static void main (String[] args) {
|
||||||
|
Scanner f;
|
||||||
|
try {
|
||||||
|
f = Part1.readFile(args[0]);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("File not found, stopping execution");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<Integer> sums = new ArrayList<Integer>();
|
||||||
|
Integer runningSum = 0;
|
||||||
|
while (f.hasNextLine()) {
|
||||||
|
String l = f.nextLine();
|
||||||
|
if (l.equals("")) {
|
||||||
|
sums.add(runningSum);
|
||||||
|
runningSum = 0;
|
||||||
|
} else {
|
||||||
|
runningSum = runningSum + Integer.parseInt(l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.close();
|
||||||
|
|
||||||
|
Iterator<Integer> i = sums.iterator();
|
||||||
|
Integer largest = Integer.MIN_VALUE;
|
||||||
|
while (i.hasNext()) {
|
||||||
|
Integer sum = i.next();
|
||||||
|
if (sum > largest) {
|
||||||
|
largest = sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.print("The largest value is: ");
|
||||||
|
System.out.println(largest);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Scanner readFile(String file) throws FileNotFoundException {
|
||||||
|
File f = new File(file);
|
||||||
|
Scanner s = new Scanner(f);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue