-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHolidayCode4.java
More file actions
131 lines (97 loc) · 3.5 KB
/
HolidayCode4.java
File metadata and controls
131 lines (97 loc) · 3.5 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.ArrayList;
import java.io.*;
public class HolidayCode4 {
public static void main(String[] args){
try{
File file = new File("rooms.txt");
FileReader fileReader = new FileReader(file);
BufferedReader input = new BufferedReader(fileReader);
String line;
int total = 0;
//97 to 122
while((line = input.readLine()) != null){
String[] parts = line.split("-");
String spec = parts[parts.length - 1];
String[] numCheck = spec.split("\\[");
String sectorNum = numCheck[0];
String check = numCheck[1];
String newCheck = check.substring(0, check.length() - 1);
//System.out.println(newCheck);
int shift = Integer.parseInt(sectorNum);
shift = shift % 26;
String north = "north";
String newNorth = "";
for(int i = 0; i < north.length(); i++){
int num = (int) north.charAt(i);
//num += shift;
num -= shift;
if(num < 97){
int temp = 96 - num;
//num -= 122;
num = 122 - temp;
}
char toAdd = (char) num;
//System.out.println(toAdd);
newNorth += toAdd;
}
//System.out.println(newNorth);
String whole = "";
for(int i = 0; i < parts.length - 1; i++){
whole += parts[i];
}
if(whole.contains(newNorth)){
System.out.println("HERE: " + sectorNum);
}
//Get five most used letters. If tied, order
ArrayList<Character> letters = new ArrayList<Character>();
ArrayList<Integer> freq = new ArrayList<Integer>();
for(int i = 0; i < whole.length(); i++){
if(!letters.contains(whole.charAt(i))){
letters.add(whole.charAt(i));
freq.add(1);
} else {
int temp = freq.get(letters.indexOf(whole.charAt(i)));
temp++;
freq.set(letters.indexOf(whole.charAt(i)), temp);
}
}
//System.out.println(letters.toString());
//System.out.println(freq.toString());
boolean valid = true;
for(int i = 0; i < 5; i++){
char test = newCheck.charAt(i);
if(letters.contains(test)){
int times = freq.get(letters.indexOf(test));
//char inCase = letters.get(letters.indexOf(test));
freq.remove(letters.indexOf(test));
letters.remove(letters.indexOf(test));
for(int j = 0; j < freq.size(); j++){
if(freq.get(j) < times){
//Nothing
} else if(freq.get(j) == times){
//Alphabetical
int test1 = (int) test;
int test2 = (int) letters.get(j);
if(test1 >= test2){
valid = false;
}
} else {
//Invalid.
valid = false;
}
}
} else {
//System.out.print(test);
valid = false;
}
}
if(valid){
total += Integer.parseInt(sectorNum);
}
}
System.out.println(total);
} catch (IOException error) { //Need this for using buffered readers
error.printStackTrace();
}
}
}