diff --git a/day_01/java/part_1.java b/day_01/java/part_1.java new file mode 100644 index 0000000..3027e44 --- /dev/null +++ b/day_01/java/part_1.java @@ -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 sums = new ArrayList(); + 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 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; + } +} \ No newline at end of file