Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Map<Integer, List<Range>> asleepTime =
- shifts.stream()
- .collect(Collectors.groupingBy(s -> s.getGuard(), Collectors.flatMapping(s -> s.getRanges().stream().filter(r -> r.getState() == State.ASLEEP), Collectors.toList())));
- int asleepMost = asleepTime.entrySet().stream()
- .max(Comparator.comparing(e -> e.getValue().stream().mapToInt(r -> r.getEnd() - r.getStart()).sum()))
- .map(Entry::getKey)
- .get();
- int[] sleepCount = new int[60];
- List<Range> ranges = asleepTime.get(asleepMost);
- for (Range r : ranges) {
- for (int i = r.getStart(); i < r.getEnd(); i++) {
- sleepCount[i]++;
- }
- }
- int maxIdx = -1, max = -1;
- for (int i = 0; i < sleepCount.length; i++) {
- if (sleepCount[i] > max) {
- maxIdx = i;
- }
- }
- int part1 = asleepMost * maxIdx;
Advertisement
Add Comment
Please, Sign In to add comment