-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventofcodeDay5Part2.java
More file actions
55 lines (49 loc) · 1.59 KB
/
Copy pathadventofcodeDay5Part2.java
File metadata and controls
55 lines (49 loc) · 1.59 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
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.lang.Math;
public class adventofcodeDay5Part2 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
File readFile = new File("/home/piyi/input.txt");
try{
Scanner fileReader = new Scanner(readFile);
int max = 0;
int seatID = 0;
List<Integer> seatIDs = new ArrayList<>();
while (fileReader.hasNextLine()) {
String data = fileReader.nextLine();
String[] arrOfStr = data.split("", -2);
int column = 0;
int row = 0;
for( int i=0; i < 7; i++ ){
if( arrOfStr[i].equals("B"))
{
row = row + (int) Math.pow(2, 6-i);
}
}
for( int i=7; i < 10; i++ ){
if( arrOfStr[i].equals("R"))
{
column = column + (int) Math.pow(2, 9-i);
}
}
seatID = row * 8 + column;
seatIDs.add(seatID);
}
fileReader.close();
for (int i = Collections.min(seatIDs); i<Collections.max(seatIDs);i++){
if (seatIDs.contains(i-1) && seatIDs.contains(i+1) && !seatIDs.contains(i)){
System.out.println(i);
break;
}
}
}
catch(Exception E){
System.out.println(E);
}
}
}