tterrag1098

Untitled

Dec 3rd, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1.  
  2. Map<Integer, List<Range>> asleepTime =
  3. shifts.stream()
  4. .collect(Collectors.groupingBy(s -> s.getGuard(), Collectors.flatMapping(s -> s.getRanges().stream().filter(r -> r.getState() == State.ASLEEP), Collectors.toList())));
  5.  
  6. int asleepMost = asleepTime.entrySet().stream()
  7. .max(Comparator.comparing(e -> e.getValue().stream().mapToInt(r -> r.getEnd() - r.getStart()).sum()))
  8. .map(Entry::getKey)
  9. .get();
  10.  
  11. int[] sleepCount = new int[60];
  12. List<Range> ranges = asleepTime.get(asleepMost);
  13. for (Range r : ranges) {
  14. for (int i = r.getStart(); i < r.getEnd(); i++) {
  15. sleepCount[i]++;
  16. }
  17. }
  18.  
  19. int maxIdx = -1, max = -1;
  20. for (int i = 0; i < sleepCount.length; i++) {
  21. if (sleepCount[i] > max) {
  22. maxIdx = i;
  23. }
  24. }
  25.  
  26. int part1 = asleepMost * maxIdx;
Advertisement
Add Comment
Please, Sign In to add comment