-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (126 loc) · 5.41 KB
/
index.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import fs from 'fs'
import path from 'path'
const fileName = process.env.npm_lifecycle_script.match(/\".+\"/)[0].replace(/"/g, '')
const filePath = path.join(__dirname, `./data/${fileName}.txt`)
let number = 0
let numberData
fs.readFile(filePath, { encoding: 'utf-8' }, (err, data) => {
if (!err) {
const librariesTotal = parseInt(data.match(/^.*/)[0].split(' ')[1])
const daysTotal = parseInt(data.match(/^.*/)[0].split(' ')[2])
const scores = data.match(/\n.*/)[0].replace(/\n/, '').split(' ')
const booksTotal = scores.length
const libraryData = data.replace(/^.*\n.*/, '').match(/\n.*\n.*/g)
const libraries = libraryData.map((thisLibrary, i) => ({
id: i,
signup: parseInt(thisLibrary.match(/\n.*/)[0].replace(/\n/, '').split(' ')[1]),
shipCount: parseInt(thisLibrary.match(/\n.*/)[0].replace(/\n/, '').split(' ')[2]),
books: thisLibrary.match(/\n.*/g)[1].replace(/\n/, '').split(' ').map(book => ({
id: book,
value: parseInt(scores[parseInt(book)])
})).sort((book1, book2) => {
// console.log(book1.value, 'hellooo')
// console.log(book2.value)
return book2.value - book1.value
})
}))
const libraryQueue = compute_metrics(libraries, scores)
libraryQueue.pop()
// console.log(libraryQueue[libraryQueue.length -1])
libraryQueue.sort((lib1, lib2) => {
// console.log(lib1.x, lib2.x)
return lib2.x - lib1.x
})
// libraryQueue.forEach(l => console.log(l.id, l.score))
let daysCurrent = 0
const libraryScanned = []
let submittedBooks = {}
let currentIndex = 0
const bookIds = {}
// let isSettingUp = false
const activeLibraries = [];
let daysUntilNextLibrary = libraryQueue[0].signup;
for (let i = 0; i < daysTotal; i++) {
if (!daysUntilNextLibrary) {
activeLibraries.push(libraryQueue[activeLibraries.length])
submittedBooks[libraryQueue[activeLibraries.length - 1].id] = []
if (libraryQueue[activeLibraries.length]) {
daysUntilNextLibrary = libraryQueue[activeLibraries.length].signup
} else {
daysUntilNextLibrary = Infinity
}
}
activeLibraries.forEach((library, libraryIndex) => {
// console.log(bookIds)
library.books = library.books.filter(book => !bookIds[book.id])
if (library.books.length) {
for (let i = 0; i < library.shipCount; i++) {
if (library.books[i]) {
// console.log('adding', library.books[i].id, 'from', libraryQueue[libraryIndex].id)
bookIds[library.books[i].id] = true
submittedBooks[libraryQueue[libraryIndex].id].push(library.books[i])
}
}
}
})
daysUntilNextLibrary--
}
// while (daysCurrent < daysTotal && libraryQueue[currentIndex]) {
// console.log(currentIndex)
// libraryQueue.push(libraryQueue[currentIndex].id)
// daysCurrent+=libraryQueue[currentIndex].signup
// submittedBooks = [
// ]
// currentIndex++
// }
// console.log(submittedBooks)
const outputLibraries = activeLibraries.filter(library => submittedBooks[parseInt(library.id)].length)
const output = `${outputLibraries.length}
${
outputLibraries.map((library, i) => `${library.id} ${submittedBooks[parseInt(library.id)].length}
${submittedBooks[parseInt(library.id)].map(({id}) => id).join(' ')}`).join('\n')
}`
console.log(Object.keys(submittedBooks).reduce((acc, lib) =>
acc + submittedBooks[lib].reduce((acc, { id }) => parseInt(scores[id]) + acc, 0), 0))
fs.writeFile(`./data/${fileName}.out`, output, (err) => {
if(err) {
return console.log(err);
}
// console.log(`File saved to ./data/${fileName}.out`);
});
// console.log(libraryQueue.length)
// console.log(libraryQueue)
// num of libs (libraryQueue.length)
// [
// [lib id] [num of books it will send]
// id1 id2 id3
// ]
// console.log(JSON.stringify(libraries))
// console.log(libraryQueue)
// 6 2 7 // There are 6 books, 2 libraries, and 7 days for scanning
// 1 2 3 6 5 4 // The scores of the books are 1, 2, 3, 6, 5, 4 (in order).
// 5 2 2 // Library 0 has 5 books, the signup process takes 2 days
// and the library can ship 2 books per day.
// 0 1 2 3 4 // The books in library 0 are: book 0, book 1, book 2, book 3, and book 4.
// 4 3 1 // Library 1 has 4 books, the signup process takes 3 days
// and the library can ship 1 book per day.
// 0 2 3 5 // The books in library 1 are: book 3, book 2, book 5 and book 0.
} else {
console.log('lol that dunt work', err)
}
})
function compute_metrics(libraries, scores) {
return libraries.map(lib => {
const sum_of_scores = lib.books
.map(({id}) => parseInt(scores[id]))
.reduce((a, b) => a + b, 0);
const num_books = lib.books.length;
// console.log(sum_of_scores, num_books,lib.shipCount, lib.signup)
const score = - lib.signup;
// console.log(score)
return {
...lib,
x: score
};
});
}