1
0
Fork 0
Advent2021/Day 1/Justin/Java/increaseCounter.java

45 lines
1.8 KiB
Java
Raw Permalink Normal View History

2021-12-01 06:38:01 +00:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
2021-12-01 06:48:02 +00:00
import org.json.JSONArray; //requires external "org.json"
import org.json.JSONTokener; //requires external "org.json"
2021-12-01 06:38:01 +00:00
class increaseCounter{
public static void main(String[] args){
2021-12-03 02:27:00 +00:00
File input = new File("Day 1/Justin/Java/input.json");
2021-12-01 06:38:01 +00:00
try{
2021-12-01 06:48:02 +00:00
// Get the file, into an inputstream, and give it to json tokenizer
2021-12-01 06:38:01 +00:00
InputStream fileIn = new FileInputStream(input);
JSONTokener inputJson = new JSONTokener(fileIn);
2021-12-01 06:48:02 +00:00
// get the only object, which is a jsonarray
2021-12-01 06:38:01 +00:00
JSONArray values = (JSONArray)inputJson.nextValue();
2021-12-01 06:48:02 +00:00
2021-12-01 06:38:01 +00:00
//System.out.println(inputArray.toString());
// Get the amount of times it increases
int increaseCount = 0;
for(int i = 1; i < values.length(); i++)
2021-12-01 06:48:02 +00:00
if(values.getInt(i-1) < values.getInt(i)) // if array[i-1] is less then array[i]
increaseCount++;
2021-12-01 06:38:01 +00:00
System.out.println(increaseCount);
// Get the amount of times it increase by three-measure sliding increments
int increaseCount3 = 0;
for(int i = 3; i < values.length(); i++){
2021-12-01 06:48:02 +00:00
int winA = values.getInt(i - 3) + values.getInt(i - 2) + values.getInt(i - 1); // get array[i-3] + array[i-2] + array[i-1] = "a" window
int winB = values.getInt(i - 2) + values.getInt(i - 1) + values.getInt(i); // get array[i-2] + array [i-1] + array[i] = "b" window
2021-12-01 06:38:01 +00:00
if(winA < winB)
increaseCount3++;
}
System.out.println(increaseCount3);
2021-12-01 06:48:02 +00:00
2021-12-01 06:38:01 +00:00
} catch(FileNotFoundException e){
e.printStackTrace();
}
}
}