daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest Oct 23rd, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class Sick {
  4.     public static void main(String[] args){
  5.     String[] input = new String[]{"Wilson SICK PaloAlto DC London PaloAlto",
  6.             "Yun HEALTHY PaloAlto", "Ali RECOVERING DC DC DC London",
  7.             "Jasmine HEALTHY London"};
  8.         String[] input2 = new String[]{"Wilson SICK PaloAlto DC London",
  9.                 "Yun HEALTHY PaloAlto", "Ali RECOVERING DC DC London",
  10.                 "Jasmine HEALTHY London"};
  11.         String[] input3 = new String[]{"Lee HEALTHY Seattle"};
  12.     Sick s = new Sick();
  13.     String[] ans = s.allHealthy(input3);
  14.     for(String a : ans){
  15.         System.out.println(a);
  16.     }
  17.     }
  18.  
  19.  
  20.     public String[] allHealthy(String[] everyone){
  21.         Map<String, List<String>> personItenariesMap = new HashMap<>();
  22.         Map<String, List<String>> personHealthStateMap = new HashMap<>();
  23.         Map<String, Set<String>> locationToPeopleMap = new HashMap<>();
  24.         for (String person : everyone){
  25.             String[] personWithLocations = person.split(" ");
  26.             String name = personWithLocations[0];
  27.             String personState = personWithLocations[1];
  28.             List<String> stateList = new ArrayList<>();
  29.             List<String> locationList = new ArrayList<>();
  30.             for (int i = 2; i < personWithLocations.length ; i ++){
  31.                 if (i == 2){
  32.                     updateLocation(name, "", personWithLocations[i], locationToPeopleMap);
  33.                 }
  34.                 locationList.add(personWithLocations[i]);
  35.             }
  36.             stateList.add(personState);
  37.             personItenariesMap.put(name, locationList);
  38.             personHealthStateMap.put(name, stateList);
  39.         }
  40.         int day = 1;
  41.         boolean everyHealthy = false;
  42.         while (day < 365 && !everyHealthy){
  43.             everyHealthy = true;
  44.  
  45.             for (String person : personItenariesMap.keySet()){
  46.                 int personLocationIndex = -1;
  47.                 if (day  <= personItenariesMap.get(person).size()){
  48.                     personLocationIndex = day-1;
  49.                 } else {
  50.                     personLocationIndex = (day-1) % personItenariesMap.get
  51.                             (person).size() ;
  52.                 }
  53.                 String personLocation = personItenariesMap.get(person).get(personLocationIndex);
  54.                 boolean hasSickPpl = arePplSickAtLocation(personLocation,
  55.                         locationToPeopleMap, personHealthStateMap,day);
  56.                 String nextDayState = nextDayState(personHealthStateMap.get(person).get(personHealthStateMap.get(person).size()-1), hasSickPpl);
  57.                 if (!nextDayState.equals("HEALTHY")){
  58.                     everyHealthy = false;
  59.                 }
  60.                 personHealthStateMap.get(person).add(nextDayState);
  61.             }
  62.             for (String person : personItenariesMap.keySet()){
  63.                 int personPreviousLocationIndex = -1;
  64.                 if (day  <= personItenariesMap.get(person).size()){
  65.                     personPreviousLocationIndex = day -1;
  66.                 } else {
  67.                     personPreviousLocationIndex = (day-1) % personItenariesMap
  68.                             .get(person).size() ;
  69.                 }
  70.  
  71.                 int personNextLocationIndex = -1;
  72.                 if (day +1 <= personItenariesMap.get(person).size()){
  73.                     personNextLocationIndex = day;
  74.                 } else {
  75.                     personNextLocationIndex = (day  % personItenariesMap
  76.                             .get(person).size());
  77.                 }
  78.  
  79.                 String personPrevLocation = personItenariesMap.get(person).get(personPreviousLocationIndex);
  80.                 String personNextLocation = personItenariesMap.get(person).get(personNextLocationIndex);
  81.                 // in the end, update location
  82.                 updateLocation(person, personPrevLocation, personNextLocation, locationToPeopleMap);
  83.             }
  84.             day++;
  85.         }
  86.         String[] days = new String[day + 2];
  87.         List<String> persons = new ArrayList<>();
  88.         StringBuilder sb = new StringBuilder();
  89.         for (String person : personHealthStateMap.keySet()){
  90.             persons.add(person);
  91.             sb.append(person);
  92.             sb.append(" ");
  93.         }
  94.  
  95.         days[0] = sb.toString().trim();
  96.         for (int i = 0; i < day ; i ++){
  97.             StringBuilder sb2 = new StringBuilder();
  98.             for (String p : persons){
  99.                 sb2.append(personHealthStateMap.get(p).get(i));
  100.                 sb2.append(" ");
  101.             }
  102.             days[i + 1] = sb2.toString().trim();
  103.         }
  104.         days[days.length - 1] = day + "";
  105.         return days;
  106.     }
  107.  
  108.     public boolean arePplSickAtLocation(String location, Map<String,
  109.             Set<String>> pplAtLocation, Map<String, List<String>>
  110.             personHealthStateMap,int day){
  111.         Set<String> people = pplAtLocation.get(location);
  112.         for (String person : people){
  113.             String currentState = personHealthStateMap.get(person).get(day-1);
  114.             if (!currentState.equals("HEALTHY")){
  115.                 return true;
  116.             }
  117.         }
  118.         return false;
  119.     }
  120.  
  121.     public void updateLocation(String person, String prevLocation, String nextLocation, Map<String, Set<String>> locationToPeopleMap){
  122.         Set<String> personsInCurrentLoc = locationToPeopleMap.getOrDefault(prevLocation, Collections.EMPTY_SET);
  123.         personsInCurrentLoc.remove(person);
  124.         if (!locationToPeopleMap.containsKey(nextLocation)){
  125.             locationToPeopleMap.put(nextLocation, new HashSet<>());
  126.         }
  127.         locationToPeopleMap.get(nextLocation).add(person);
  128.     }
  129.  
  130.     public String nextDayState(String state, boolean hasSick){
  131.         if ("SICK".equals(state)){
  132.             return "RECOVERING";
  133.         }
  134.         if ("RECOVERING".equals(state)){
  135.             return "HEALTHY";
  136.         }
  137.         if ("HEALTHY".equals(state) && hasSick){
  138.             return "SICK";
  139.         }
  140.         return "HEALTHY";
  141.     }
  142. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top