-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 5.java
100 lines (82 loc) · 2.54 KB
/
Day 5.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
class Main {
public static void main(String[] args) {
String[] temp = populateArray();
List<Integer> allSeats = new ArrayList<Integer>();
int highestSeatNum = 0;
for (int i = 0; i < 868; i++){
int seat = 128;
int width = 8;
int upper = 128;
int lowerWide = 1;
int upperWide = 8;
int row = 0;
int column = 0;
int lower = 1;
for (int j = 0; j < 10; j ++){
//FBFBBFFRLR
if (temp[i].substring(j,j+1).equals("B")){
seat/=2;
lower = upper - seat + 1;
} else if (temp[i].substring(j,j+1).equals("F")){
seat/=2;
upper = lower + seat-1;
}
//System.out.println("seat: " + seat);
//System.out.println("upper: " + upper);
//System.out.println("lower: " + lower);
//System.out.println("row: " + row);
if (temp[i].substring(j,j+1).equals("R")){
width/=2;
lowerWide = upperWide - width + 1;
} else if (temp[i].substring(j,j+1).equals("L")){
width/=2;
upperWide = lowerWide + width-1;
}
//System.out.println("seaWidtht: " + width);
//System.out.println("upperWidth: " + upperWide);
//System.out.println("lowerWidth: " + lowerWide);
row = lower;
row--;
column = upperWide;
column--;
//System.out.println("column: " + column);
//System.out.println("row: " + row);
}
int temphigh = ((row * 8) + column);
allSeats.add(temphigh);
if (highestSeatNum < temphigh){
highestSeatNum = temphigh;
}
}
System.out.println("highest seat num: " + highestSeatNum);
Collections.sort(allSeats);
System.out.println(allSeats);
for (int i = 70; i < 938; i++){
if (!allSeats.contains(i)){
System.out.println("Missing num: " + i);
}
}
}
public static String[] populateArray(){
String[] array = new String[868];
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;
}
}