aoc/2021/day01Part2.java

67 lines
1.8 KiB
Java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class day01Part2 {
public static void main(String[] args) throws IOException {
int hasIncreasedCounter = 0;
int lineCounter = 1;
int firstnum = 0;
int secondnum = 0;
int thirdnum = 0;
int oldsum = 0;
int newsum = 0;
String line; // convert to int or something
BufferedReader in = new BufferedReader(new FileReader("day01-input.txt"));
line = in.readLine();
while (line != null) {
if (lineCounter == 1) { // first lines in file
firstnum = Integer.valueOf(line).intValue();
}
else if (lineCounter == 2) {
secondnum = Integer.valueOf(line).intValue();
}
else if (lineCounter == 3) {
thirdnum = Integer.valueOf(line).intValue();
}
else {
oldsum = firstnum + secondnum + thirdnum;
// swapping done here
firstnum = secondnum;
secondnum = thirdnum;
// read new line as thirdnum
thirdnum = Integer.valueOf(line).intValue();
newsum = firstnum + secondnum + thirdnum;
System.out.println("Comparing " + oldsum + " and " + newsum);
if (newsum > oldsum) {
System.out.println("Yes!");
hasIncreasedCounter++;
}
else {
System.out.println("Not bigger");
}
// oldsum = newsum; // swap to continue
}
lineCounter++;
// continue
line = in.readLine();
}
in.close(); // bye
System.out.println("Final is " + hasIncreasedCounter);
}
}