init
This commit is contained in:
78
Library/index.js
Normal file
78
Library/index.js
Normal file
@@ -0,0 +1,78 @@
|
||||
var fs = require('fs');
|
||||
|
||||
let inputFile = fs.readFileSync('input.txt', 'utf-8');
|
||||
|
||||
let myArray = inputFile.split('\n').map((line) => line.split(' '));
|
||||
|
||||
const TOTAL_SIGNUP_TIME = parseInt(myArray[0][2]);
|
||||
|
||||
let libraryScores = [];
|
||||
let libraryBooksScore = [];
|
||||
let maxLibraries = [];
|
||||
|
||||
for (let i = 2; i < myArray.length; i += 2) {
|
||||
const libraryBooks = myArray[i + 1];
|
||||
|
||||
const daysToScan = TOTAL_SIGNUP_TIME - parseInt(myArray[i][1]);
|
||||
const maxBooks = parseInt(myArray[i][2]) * daysToScan;
|
||||
|
||||
let libraryBook = [];
|
||||
|
||||
const libraryScore = libraryBooks
|
||||
.map((bookIndex) => parseInt(myArray[1][bookIndex]))
|
||||
.sort()
|
||||
.reverse()
|
||||
.slice(0, maxBooks)
|
||||
.map((bookValue) => {
|
||||
myArray[1].forEach((v, i) => {
|
||||
if (
|
||||
v.toString() === bookValue.toString() &&
|
||||
libraryBooks.includes(i.toString())
|
||||
) {
|
||||
libraryBook.push(i);
|
||||
}
|
||||
});
|
||||
return bookValue;
|
||||
})
|
||||
.reduce((result, item) => result + item, 0);
|
||||
|
||||
libraryScores.push(libraryScore);
|
||||
libraryBooksScore.push(libraryBook);
|
||||
}
|
||||
|
||||
let copyLibraryScores = [...libraryScores];
|
||||
copyLibraryScores = copyLibraryScores.sort().reverse();
|
||||
copyLibraryScores.forEach((score) => {
|
||||
libraryScores.forEach((s, libraryIndex) => {
|
||||
if (s.toString() === score.toString()) {
|
||||
maxLibraries.push(libraryIndex);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let totalNumberofLibraries = 0;
|
||||
let totalSignupTime = TOTAL_SIGNUP_TIME;
|
||||
|
||||
while (totalSignupTime > 0 && totalNumberofLibraries < maxLibraries.length) {
|
||||
totalSignupTime -= parseInt(
|
||||
myArray[maxLibraries[totalNumberofLibraries] * 2 + 2][1]
|
||||
);
|
||||
totalNumberofLibraries++;
|
||||
}
|
||||
|
||||
let booksToSendScanning = [];
|
||||
let totalSignupYet = TOTAL_SIGNUP_TIME;
|
||||
for (let j = 0; j < maxLibraries.length; j++) {
|
||||
totalSignupYet -= myArray[maxLibraries[j] * 2 + 2][1];
|
||||
const maxCurrentBooks =
|
||||
(TOTAL_SIGNUP_TIME - totalSignupYet) * myArray[maxLibraries[j] * 2 + 2][2];
|
||||
booksToSendScanning.push([
|
||||
...libraryBooksScore[maxLibraries[j]].slice(0, maxCurrentBooks),
|
||||
]);
|
||||
}
|
||||
|
||||
console.log(`${totalNumberofLibraries}`);
|
||||
for (let k = 0; k < booksToSendScanning.length; k++) {
|
||||
console.log(`${maxLibraries[k]} ${booksToSendScanning[k].length}`);
|
||||
console.log(`${booksToSendScanning[k].join(' ')}`);
|
||||
}
|
||||
6
Library/input.txt
Normal file
6
Library/input.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
6 2 7
|
||||
1 2 3 6 5 4
|
||||
5 2 2
|
||||
0 1 2 3 4
|
||||
4 3 1
|
||||
3 2 5 0
|
||||
5
Library/result.txt
Normal file
5
Library/result.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
2
|
||||
0 5
|
||||
3 4 2 1 0
|
||||
1 1
|
||||
5
|
||||
132
Library/solution.js
Normal file
132
Library/solution.js
Normal file
@@ -0,0 +1,132 @@
|
||||
const fs = require('fs');
|
||||
|
||||
const file = fs.readFileSync('input.txt', 'utf-8');
|
||||
const result = file.split('\n').map((item) => item.split(' '));
|
||||
|
||||
const readyLibraries = [];
|
||||
|
||||
const set = {
|
||||
bookNumber: parseInt(result[0][0]),
|
||||
deadline: parseInt(result[0][2]),
|
||||
booksScore: [...result[1].map((it) => parseInt(it, 10))],
|
||||
libraries: [],
|
||||
};
|
||||
|
||||
// Loop on all libraries
|
||||
for (let i = 0; i < result[0][1]; i++) {
|
||||
const firstRow = result[i * 2 + 2];
|
||||
const secondRow = result[i * 2 + 3];
|
||||
|
||||
set.libraries.push({
|
||||
daysToSignUp: parseInt(firstRow[1]),
|
||||
scanPerDay: parseInt(firstRow[2]),
|
||||
// Sort & insert list of books in descending order of highest value per lib
|
||||
books: [
|
||||
...secondRow
|
||||
.map((it) => parseInt(it, 10))
|
||||
.sort((a, b) => set.booksScore[b] - set.booksScore[a]),
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
let signupLibrary = null;
|
||||
|
||||
for (let j = 0; j < set.deadline; j++) {
|
||||
if (!signupLibrary) {
|
||||
signupLibrary = findLibraryToSetup(j);
|
||||
}
|
||||
|
||||
if (signupLibrary) {
|
||||
signupLibrary.daysToSignUp--;
|
||||
|
||||
if (signupLibrary.daysToSignUp === 0) {
|
||||
// Keep only max number of books possible
|
||||
const scannedBooks = set.libraries[signupLibrary.id].books.slice(
|
||||
0,
|
||||
signupLibrary.booksCount
|
||||
);
|
||||
|
||||
readyLibraries.push({
|
||||
id: signupLibrary.id,
|
||||
books: scannedBooks,
|
||||
});
|
||||
|
||||
set.libraries[signupLibrary.id] = undefined;
|
||||
signupLibrary = null;
|
||||
removeDuplicates(scannedBooks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findLibraryToSetup(currentDay) {
|
||||
const availableLibraries = set.libraries
|
||||
.map((library, index) =>
|
||||
library !== undefined
|
||||
? {
|
||||
id: index,
|
||||
value: 0,
|
||||
daysToSignUp: library.daysToSignUp,
|
||||
}
|
||||
: undefined
|
||||
)
|
||||
.filter((item) => item !== undefined);
|
||||
|
||||
availableLibraries.forEach((library) => {
|
||||
const activeDays = set.deadline - currentDay - library.daysToSignUp;
|
||||
const trueLibrary = set.libraries[library.id];
|
||||
|
||||
// Find minimum of max books per day * days available OR amount of books in lib
|
||||
const scannedBooks = Math.min(
|
||||
activeDays * trueLibrary.scanPerDay,
|
||||
trueLibrary.books.length
|
||||
);
|
||||
|
||||
library.booksCount = scannedBooks;
|
||||
trueLibrary.books.slice(0, scannedBooks).forEach((id) => {
|
||||
library.value += set.booksScore[id];
|
||||
});
|
||||
|
||||
// Get a kind of score ratio
|
||||
library.value /= library.daysToSignUp;
|
||||
});
|
||||
|
||||
const sorted = availableLibraries.sort((a, b) => {
|
||||
// If equal value, put the one with the highest value first
|
||||
if (b.value === a.value) {
|
||||
return b.daysToSignUp - a.daysToSignUp;
|
||||
}
|
||||
|
||||
return b.value - a.value;
|
||||
});
|
||||
|
||||
return sorted[0];
|
||||
}
|
||||
|
||||
function removeDuplicates(booksToRemove) {
|
||||
set.libraries.forEach((library) => {
|
||||
if (library) {
|
||||
library.books = library.books.filter(
|
||||
(book) => !booksToRemove.some((book1) => book1 === book)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let results = 0;
|
||||
|
||||
readyLibraries.forEach((lib) => {
|
||||
lib.books.forEach((book) => {
|
||||
results += set.booksScore[book];
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`score: ${results}`);
|
||||
|
||||
const finalResult = [readyLibraries.length];
|
||||
readyLibraries.forEach((library) => {
|
||||
finalResult.push(`${library.id} ${library.books.length}`);
|
||||
finalResult.push(library.books.join(' '));
|
||||
});
|
||||
|
||||
// fs.writeFileSync(files[process.env.FILE].output, finalResult.join("\n"))
|
||||
fs.writeFileSync('result.txt', finalResult.join('\n'));
|
||||
Reference in New Issue
Block a user