-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 13.java
71 lines (66 loc) · 1.76 KB
/
Day 13.java
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
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
class Main {
private static int time = 1000053;
public static void main(String[] args) {
String[] x = populateArray();
String[] arrOfStr = x[0].split(",");
//System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(arrOfStr));
firstStar(arrOfStr);
}
public static String[] firstStar(String[] arrOfStr){
boolean found = false;
int smallestTime = 999999999;
int foundTime = 0;
int bus = 0;
int id = 0;
for (int i = 0; i < arrOfStr.length; i++){
//System.out.println(i);
time = 1000053;
while (!found){
System.out.println((arrOfStr[i]));
if(!arrOfStr[i].equals("x")){
id = Integer.parseInt(arrOfStr[i]);
if (time%id==0){
found = true;
foundTime=time;
break;
} else {
time++;
}
} else {
break;
}
}
if (foundTime < smallestTime){
smallestTime = foundTime;
bus = id;
}
found = false;
}
System.out.println("bus: " + bus);
System.out.println("time:" + smallestTime);
return arrOfStr;
}
public static String[] populateArray(){
String[] array = new String[1];
int index = 0;
try {
File myObj = new File("puzzle.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
array[index] = data;
index++;
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return array;
}
}