Advertisement
dzocesrce

[NP] STOP CORONA!!!!!

Apr 30th, 2025
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.72 KB | None | 0 0
  1. //STOP CORONA 2020
  2. //Must admit, battling the corona virus was personally easier than solving this task
  3. //I feel like it's 95% okay written, but 95% is far from solid accuracy when you're up against the likes of COVID-19
  4.  
  5. import java.time.Duration;
  6. import java.time.LocalDateTime;
  7. import java.util.*;
  8. import java.time.Duration;
  9. import java.time.LocalDateTime;
  10. import java.util.*;
  11. import java.util.stream.Collectors;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. class UserAlreadyExistException extends Exception{
  15.     public UserAlreadyExistException(String id) {
  16.         super(String.format("User with id %s already exists.",id));
  17.     }
  18. }
  19.  class User {
  20.     String id;
  21.     String name;
  22.     LocalDateTime coronaTimestamp;
  23.     List<ILocation> locations;
  24.  
  25.     public User(String id, String name) {
  26.         this.id = id;
  27.         this.name = name;
  28.         this.coronaTimestamp= LocalDateTime.now();
  29.         this.locations = new ArrayList<>();
  30.     }
  31.  
  32.     @Override
  33.     public String toString() {
  34.         return "User{" +
  35.                 "id='" + id + '\'' +
  36.                 ", name='" + name + '\'' +
  37.                 ", coronaTimestamp=" + coronaTimestamp;
  38.     }
  39.  
  40.     public String getName() {
  41.         return name;
  42.     }
  43.  
  44.     public LocalDateTime getCoronaTimestamp() {
  45.         return coronaTimestamp;
  46.     }
  47.  
  48.     public String getId() {
  49.         return id;
  50.     }
  51.  
  52.     public void setLocations(List<ILocation> locations) {
  53.         this.locations = locations;
  54.     }
  55.  
  56.     public void setCoronaTimestamp(LocalDateTime coronaTimestamp) {
  57.         this.coronaTimestamp = coronaTimestamp;
  58.     }
  59. }
  60. class StopCoronaApp {
  61.     Map<String,User> users;
  62.     Map<String,List<ILocation>> locationsByUser;
  63.     List<User> detectedCases;
  64.     public StopCoronaApp() {
  65.         this.users= new HashMap<>();
  66.         this.locationsByUser = new HashMap<>();
  67.         this.detectedCases = new ArrayList<>();
  68.     }
  69.  
  70.     public void addUser(String name, String id) throws UserAlreadyExistException {
  71.         if(users.containsKey(id))
  72.             throw new UserAlreadyExistException(id);
  73.         users.putIfAbsent(id, new User(id, name));
  74.     }
  75.  
  76.     public void addLocations(String id, List<ILocation> locations) {
  77.         locationsByUser.putIfAbsent(id, new ArrayList<>(locations));
  78.         users.get(id).locations = locations;
  79.     }
  80.  
  81.     public void detectNewCase(String id, LocalDateTime timestamp) {
  82.         detectedCases.add(users.get(id));
  83.         users.get(id).setCoronaTimestamp(timestamp);
  84.     }
  85.     public Map<User, Long> getDirectContacts (User u){
  86.         Map<User,Long> directContacts = new HashMap<>();
  87.         for(User user : users.values()){
  88.             if(user.equals(u))
  89.                 continue;
  90.             long contactsMade=0l;
  91.             for(ILocation location : user.locations){
  92.                 contactsMade += u.locations.stream()
  93.                         .filter(i->Math.abs(Duration.between(i.getTimestamp(),
  94.                                 location.getTimestamp()).toMinutes())<=5&&
  95.                                 Math.abs(Math.pow((i.getLatitude()-location.getLatitude()),2)+
  96.                                         Math.pow(i.getLongitude()-location.getLongitude(),2))<=4).count();
  97.             }
  98.             directContacts.put(user,contactsMade);
  99.         }
  100.         return directContacts.entrySet().stream()
  101.                 .sorted(Comparator.comparing((Map.Entry<User,Long> entry)->entry.getValue(),Comparator.reverseOrder()))
  102.                 .collect(Collectors.toMap(
  103.                         entry->entry.getKey(),
  104.                         entry->entry.getValue(),
  105.                         (e1,e2)->e1,
  106.                         LinkedHashMap::new
  107.                 ));
  108.     }
  109.  
  110.     public Collection<User> getIndirectContacts (User u){
  111.         Map<User,Long> directContacts = getDirectContacts(u);
  112.         Set<User> indirectContacts = new HashSet<>();
  113.         for(User user : directContacts.keySet()){
  114.             Map<User,Long> directContactsFromDirectContacts = getDirectContacts(user);
  115.                 for(User indirectUser : directContactsFromDirectContacts.keySet()){
  116.                     if(directContacts.containsKey(indirectUser)){
  117.                         continue;
  118.                     }
  119.                     indirectContacts.add(indirectUser);
  120.                 }
  121.         }
  122.         return indirectContacts;
  123.     }
  124.  
  125.     public void createReport() {
  126.         int totalDirectContacts = 0;
  127.         int totalIndirectContacts = 0;
  128.         for(User user : detectedCases.stream().sorted(Comparator.comparing(User::getCoronaTimestamp)).collect(Collectors.toList())){
  129.             System.out.println(String.format("%s %s %s",
  130.                     user.name,
  131.                     user.id,
  132.                     user.coronaTimestamp));
  133.             System.out.println("Direct contacts:");
  134.             Map<User,Long> directContacts = getDirectContacts(user);
  135.             for(Map.Entry<User,Long> entry : directContacts.entrySet()){
  136.                 if(entry.getValue()==0)
  137.                     continue;
  138.                 System.out.println(String.format("%s %s %d",
  139.                         entry.getKey().getName(),
  140.                         entry.getKey().getId().substring(0,5).concat("***"),
  141.                         entry.getValue()));
  142.             }
  143.             System.out.println(String.format("Count of direct contacts: %d",
  144.                     directContacts.values().stream().mapToLong(i->i).sum()));
  145.             totalDirectContacts+=directContacts.values().stream().mapToLong(i->i).sum();
  146.             System.out.println("Indirect contacts:");
  147.             for(User indirectUser : getIndirectContacts(user)){
  148.                 System.out.println(String.format("%s %s",
  149.                         indirectUser.getName(),
  150.                         indirectUser.getId().substring(0,5).concat("***")));
  151.             }
  152.             System.out.println(String.format("Count of indirect contacts: %d",getIndirectContacts(user).size()));
  153.             totalIndirectContacts+=getIndirectContacts(user).size();
  154.         }
  155.         System.out.println(String.format("Average direct contacts: %.4f",(double)totalDirectContacts/detectedCases.size()));
  156.         System.out.println(String.format("Average indirect contacts: %.4f",(double)totalIndirectContacts/detectedCases.size()));
  157.     }
  158. }
  159.  
  160. interface ILocation{
  161.     double getLongitude();
  162.  
  163.     double getLatitude();
  164.  
  165.     LocalDateTime getTimestamp();
  166. }
  167.  
  168. public class StopCoronaTest {
  169.    
  170.     public static double timeBetweenInSeconds(ILocation location1, ILocation location2) {
  171.         return Math.abs(Duration.between(location1.getTimestamp(), location2.getTimestamp()).getSeconds());
  172.     }
  173.    
  174.     public static void main(String[] args) {
  175.         Scanner sc = new Scanner(System.in);
  176.  
  177.         StopCoronaApp stopCoronaApp = new StopCoronaApp();
  178.  
  179.         while (sc.hasNext()) {
  180.             String line = sc.nextLine();
  181.             String[] parts = line.split("\\s+");
  182.  
  183.             switch (parts[0]) {
  184.                 case "REG": //register
  185.                     String name = parts[1];
  186.                     String id = parts[2];
  187.                     try {
  188.                         stopCoronaApp.addUser(name, id);
  189.                     } catch (UserAlreadyExistException e) {
  190.                         System.out.println(e.getMessage());
  191.                     }
  192.                     break;
  193.                 case "LOC": //add locations
  194.                     id = parts[1];
  195.                     List<ILocation> locations = new ArrayList<>();
  196.                     for (int i = 2; i < parts.length; i += 3) {
  197.                         locations.add(createLocationObject(parts[i], parts[i + 1], parts[i + 2]));
  198.                     }
  199.                     stopCoronaApp.addLocations(id, locations);
  200.  
  201.                     break;
  202.                 case "DET": //detect new cases
  203.                     id = parts[1];
  204.                     LocalDateTime timestamp = LocalDateTime.parse(parts[2]);
  205.                     stopCoronaApp.detectNewCase(id, timestamp);
  206.  
  207.                     break;
  208.                 case "REP": //print report
  209.                     stopCoronaApp.createReport();
  210.                     break;
  211.                 default:
  212.                     break;
  213.             }
  214.         }
  215.     }
  216.  
  217.     private static ILocation createLocationObject(String lon, String lat, String timestamp) {
  218.         return new ILocation() {
  219.             @Override
  220.             public double getLongitude() {
  221.                 return Double.parseDouble(lon);
  222.             }
  223.  
  224.             @Override
  225.             public double getLatitude() {
  226.                 return Double.parseDouble(lat);
  227.             }
  228.  
  229.             @Override
  230.             public LocalDateTime getTimestamp() {
  231.                 return LocalDateTime.parse(timestamp);
  232.             }
  233.         };
  234.     }
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement