aoc2021day01
unknown
java
4 years ago
1.7 kB
8
Indexable
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Solution01A { public static void main(String[] args) { try { File myObj = new File(args[0]); Scanner myReader = new Scanner(myObj); int count = 0; int prev = Integer.valueOf(myReader.nextLine()); while (myReader.hasNextLine()) { int curr = Integer.valueOf(myReader.nextLine()); if (curr > prev) count++; prev = curr; } myReader.close(); System.out.println(count); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } } } import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Solution01B { public static final int SIZE = 3; public static void main(String[] args) { try { File myObj = new File(args[0]); Scanner myReader = new Scanner(myObj); int count = 0; int[] ring = new int[SIZE]; int index = 0; int sum = 0; while (myReader.hasNextLine() && index < SIZE) { ring[index] = Integer.valueOf(myReader.nextLine()); sum += ring[index]; index++; } index = 0; while (myReader.hasNextLine()) { int curr_num = Integer.valueOf(myReader.nextLine()); int curr_sum = sum - ring[index] + curr_num; if (curr_sum > sum) count++; sum = curr_sum; ring[index] = curr_num; index = (index + 1) % SIZE; } myReader.close(); System.out.println(count); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } } }
Editor is loading...