Skip to content

Instantly share code, notes, and snippets.

@JankDev
Created February 20, 2020 21:32
Show Gist options
  • Save JankDev/488bf96717222992d0f12722dc58fca8 to your computer and use it in GitHub Desktop.
Save JankDev/488bf96717222992d0f12722dc58fca8 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.*;
public class FileUtil {
public static void main(String... args) throws IOException {
Path path = Paths.get("b.txt");
BufferedReader bufferedReader = Files.newBufferedReader(path);
String[] firstLine = Pattern.compile(" ").split(bufferedReader.readLine());
int numberOfBooks = Integer.parseInt(firstLine[0]);
int numberOfLibraries = Integer.parseInt(firstLine[1]);
int deadline = Integer.parseInt(firstLine[2]);
Integer[] bookValues = Pattern.compile(" ").splitAsStream(bufferedReader.readLine()).map(Integer::parseInt).toArray(Integer[]::new);
var books = IntStream.iterate(0, i -> i < bookValues.length, i -> i + 1)
.mapToObj(i -> new Book(i, bookValues[i]))
.collect(toMap(book -> book.id, book -> book));
String[] libraryLines = Files.lines(path)
.skip(2)
.toArray(String[]::new);
var libraries = IntStream.iterate(0, i -> i < libraryLines.length, i -> i += 2)
.mapToObj(i -> new Library(
Integer.parseInt(Pattern.compile(" ").split(libraryLines[i])[1]),
Integer.parseInt(Pattern.compile(" ").split(libraryLines[i])[1]),
Pattern.compile(" ").splitAsStream(libraryLines[i])
.map(Integer::parseInt)
.map(books::get).sorted((a, b) -> b.value - a.value)
.collect(toList()))
)
.collect(toList());
var book = libraries.stream().max((l1,l2)-> (Integer) l1.book.stream().limit((deadline - l1.signupTime) * l1.booksPerDayScan).mapToInt(b -> b.value).sum() - (Integer) l2.book.stream().limit((deadline - l2.signupTime) * l2.booksPerDayScan).mapToInt(b -> b.value).sum());
System.out.println(book);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment