-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleHash.java
More file actions
150 lines (133 loc) · 6.04 KB
/
GoogleHash.java
File metadata and controls
150 lines (133 loc) · 6.04 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class GoogleHash {
public static void main(String[] args) {
//stores books that have been already shipped
HashMap<Integer, Boolean> hashMapTags = new HashMap<>();
//variables
int[] bookScore = null;
ArrayList<ArrayList<Integer>> bookInLibrary = new ArrayList<>();
ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
int[][] librariesInfo = null;
/*
librariesInfo stores the following information:
0. number of books in the library
1. how long the sign-up precess takes
2. how many books it can ship per day
3. the ID of the library
*/
//putting the file into the scanner
File inputFile = new File("B1.txt");
Scanner in = null;
try {
in = new Scanner(inputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//read the file parameters
int books = in.nextInt();
int libraries = in.nextInt();
int days = in.nextInt();
in.nextLine();
//read the book scores
bookScore = new int[books];
String lienStr = in.nextLine();
Scanner line = new Scanner(lienStr);
for(int i = 0; i < books; i++) {
bookScore[i] = line.nextInt();
}
line.close();
//read the rest of the file
librariesInfo = new int[libraries][4];
for(int i = 0; i < libraries; i++) {
//reading the libraries
lienStr = in.nextLine();
Scanner line2 = new Scanner(lienStr);
for(int j = 0; j < 3; j++) {
librariesInfo[i][j] = line2.nextInt();
}
librariesInfo[i][3] = i;//storing the number of the library
//reading the books in the libraries
lienStr = in.nextLine();
line2 = new Scanner(lienStr);
bookInLibrary.add(new ArrayList());
for(int j = 0; j < librariesInfo[i][0]; j++) {
bookInLibrary.get(i).add(line2.nextInt());//storing the books of the libraries
}
line2.close();
}
in.close();
// sorts the libraries by the signing process from lowest to highest
for (int i = 0; i < libraries-1; i++) {
for (int j = 0; j < libraries - i - 1; j++) {
if (librariesInfo[j][1] > librariesInfo[j + 1][1]) {
int[] temp22 = librariesInfo[j];
librariesInfo[j] = librariesInfo[j + 1];
librariesInfo[j + 1] = temp22;
}
}
}
//add the starting number of the amount of libraries in the file
ans.add(new ArrayList<>());
ans.get(0).add(0);
boolean signing = false;//has the file been signed
int counter = 0;//helps to track with which library we are working
int countLib = 0;//tracks when the library has been signed
for(int i = 0; i < days; i++) {//simulating days
if(!signing) {//if the signing process hasn't finished
if(librariesInfo.length > counter) {//protection from librariesInfo indexOutOfBounds
ArrayList<Integer> bookTemp = new ArrayList<>();//stores the libraries books that haven't been shaped by the oder libraries
countLib = librariesInfo[counter][1];//gets the amount of days it will take to sign
int newBooks = 0;//number of new books that haven't been shaped in the library
for (int k = 0; k < librariesInfo[counter][0]; k++) {
if (!hashMapTags.containsKey(bookInLibrary.get(librariesInfo[counter][3]).get(k))) {
/*if the book dose not exist in the hasMap then its a new book. Add that book to the hasMap,
add that book to the bookTemp and add +1 to the newBooks. If the book dose exist the do nothing.
*/
hashMapTags.put(bookInLibrary.get(librariesInfo[counter][3]).get(k), true);
newBooks++;
bookTemp.add(bookInLibrary.get(librariesInfo[counter][3]).get(k));
}
}
if(newBooks > 0){//if at least 1 new books is in the library
ans.get(0).set(0, counter+1);//new library has been added so change the number to new one
//adding the new libraries and the books that have been shaped
ans.add(new ArrayList<>());
ans.get(ans.size() - 1).add(librariesInfo[counter][3]);
ans.get(ans.size() - 1).add(newBooks);
ans.add(new ArrayList<>());
for (int a = 0; a < bookTemp.size(); a++) {
ans.get(ans.size() - 1).add(bookTemp.get(a));
}
}
counter++;
signing = true;//signing process is happening
}
}
countLib--;
if(countLib == 0) {//signing process has been complected
signing = false;
}
}
//printing the ans file out
File outputfile = new File("ans.txt");
PrintWriter out = null;
try {
out = new PrintWriter(outputfile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//ading spaces and new line brakes between numbers
for(int i = 0; i < ans.size(); i++) {
for(int j = 0; j < ans.get(i).size(); j++) {
out.print(ans.get(i).get(j) + " ");
}
out.println();
}
out.close();
}
}