-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1SonarSweep.java
More file actions
76 lines (63 loc) · 1.91 KB
/
Copy pathDay1SonarSweep.java
File metadata and controls
76 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.*;
class Day1SonarSweep {
public static void main(String args[]){
int n = 0;
int numOfLines = 2000;
int[] Data = new int[numOfLines];
int[] DataSum = new int[numOfLines];
int counter = 0;
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Day1SonarData.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console, br.readLine() means it reads one line at a time
Data[n] = Integer.parseInt(strLine);
System.out.println(Data[n]);
n++;
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
System.out.println("final n is: " + n);
//add every three together put it in an array
for(int j = 1; j < n-1; j++){
DataSum[j-1] = Data[j-1] + Data[j] + Data[j+1];
}
//compare three-add array to the previous term
for(int i = 1; i < n; i++){
if(DataSum[i] > DataSum[i-1]){
//System.out.println(Data[i] + " is bigger than " + Data[i-1]);
counter ++;
}
}
//System.out.println("Total increased times is: " + ArrayIncreaseCounter(n,Data[]));
System.out.println("Total increased times is: " + counter);
}
/*public int ArrayIncreaseCounter(int n, int Array[]){
int counter = 0;
for(int i = 1; i < n; i++){
if(Array[i] > Array[i-1]){
//System.out.println(Data[i] + " is bigger than " + Data[i-1]);
counter ++;
}
}
return counter;
}*/
}
/*import java.util.Scanner;
public class Day1SonarSweep{
public static void main(String[] args){
Scanner sc = new Scanner(System.input);
int first =
}
}*/
//FIRST ANSWER: 1502
//SECOND ANSWER: 1538