Skip to content

Instantly share code, notes, and snippets.

@Eragoo
Created February 22, 2022 19:19
Show Gist options
  • Save Eragoo/225b9811f875b127c0381a58411e9112 to your computer and use it in GitHub Desktop.
Save Eragoo/225b9811f875b127c0381a58411e9112 to your computer and use it in GitHub Desktop.
package org.example;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class App {
public static void main(String[] args) throws IOException {
File file = new File("/Users/yevhenii/Documents/projects/GoogleHashCodeTest/c_coarse.in.txt");
Scanner scanner = new Scanner(new FileInputStream(file));
int c = scanner.nextInt();
List<Client> clients = new ArrayList<>();
for (int i = 0; i < c; i++) {
scanner.next();
String likes = scanner.nextLine().trim();
scanner.next();
String dislikes = scanner.nextLine().trim();
clients.add(new Client(
i,
Stream.of(likes.split(" ")).filter(s -> !s.equals("")).collect(Collectors.toSet()),
Stream.of(dislikes.split(" ")).filter(s -> !s.equals("")).collect(Collectors.toSet())
));
}
HashMap<String, Product> products = new HashMap<>();
for (Client client : clients) {
for (String like : client.likes) {
if (!products.containsKey(like)) {
products.put(like, new Product(like, new HashSet<>(), new HashSet<>()));
}
Product product = products.get(like);
product.likes.add(client);
}
for (String dislike : client.dislikes) {
if (!products.containsKey(dislike)) {
products.put(dislike, new Product(dislike, new HashSet<>(), new HashSet<>()));
}
Product product = products.get(dislike);
product.dislikes.add(client);
}
}
List<Product> productList = new ArrayList<>(products.values());
Set<String> result;
Set<String> bestResult = new HashSet<>();
long bestScore = 0;
for (int i = 0; i < (1<< products.size()); i++) {
result = new HashSet<>();
for (int j = 0; j < products.size(); j++) {
if (((i >> j) % 2) == 1) {
result.add(productList.get(j).name);
}
// 36 >> 6 % 2
}
long currScore = score(result, clients);
if (bestScore < currScore) {
bestScore = currScore;
bestResult = result;
}
// System.out.println(score(result, clients));
// System.out.println(result);
// System.out.println("----------");
}
// Set<String> result = products.values()
// .stream()
// .filter(p -> p.dislikes.isEmpty())
// .map(p -> p.name)
// .collect(Collectors.toSet());
System.out.println(score(bestResult, clients));
System.out.println(bestResult);
int k = products.size();
System.out.println("----------");
bestScore = 0;
for (int i = 0; i < (1 << k); i++) {
Set<String> currResult = productMask(i, productList);
long currentScore = score(currResult, clients);
if (currentScore > bestScore) {
bestResult = currResult;
bestScore = currentScore;
}
}
System.out.println("==============");
System.out.println(score(bestResult, clients));
System.out.println(bestResult);
FileWriter fw = new FileWriter("output.txt");
fw.write(bestResult.size() + " ");
fw.write(String.join(" ", bestResult));
fw.close();
//
}
private static Set<String> productMask(int n, List<Product> products) {
Set<String> b = new HashSet<>();
int i = 0;
while (n != 0) {
if ((n % 2) == 1) {
b.add(products.get(i).name);
}
n = n / 2;
i++;
}
return b;
}
private static long score(Set<String> result, Collection<Client> input) {
return input.parallelStream()
.filter(client -> result.containsAll(client.likes) && result.stream().noneMatch(p -> client.dislikes.contains(p)))
.count();
}
private static class Product {
String name;
Set<Client> likes;
Set<Client> dislikes;
public Product(String name, Set<Client> likes, Set<Client> dislikes) {
this.name = name;
this.likes = likes;
this.dislikes = dislikes;
}
@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", likes=" + likes +
", dislikes=" + dislikes +
'}';
}
}
private static class Client {
int id;
Set<String> likes;
Set<String> dislikes;
public Client(int id, Set<String> likes, Set<String> dislikes) {
this.id = id;
this.likes = likes;
this.dislikes = dislikes;
}
@Override
public String toString() {
return "Client{" +
"id=" + id +
", likes=" + likes.size() +
", dislikes=" + dislikes.size() +
'}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment