Created
May 12, 2014 13:26
-
-
Save skrb/7fa1a365b25b32e7fdd3 to your computer and use it in GitHub Desktop.
“レッスンは何曜日?”をJavaでやってみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
public class Example { | |
static class LessonClass { | |
private int dayOfWeek; | |
private List<Integer> employees = new ArrayList<>(); | |
public LessonClass(int dayOfWeek) { | |
this.dayOfWeek = dayOfWeek; | |
} | |
public void addEmployee(int employee) { | |
employees.add(employee); | |
} | |
public int getDayOfWeek() { | |
return dayOfWeek; | |
} | |
public List<Integer> getEmployees() { | |
return employees; | |
} | |
} | |
static class Request { | |
private int employee; | |
private int[] dayOfWeeks; | |
private Request(int employee, int[] dayOfWeeks) { | |
this.employee = employee; | |
this.dayOfWeeks = dayOfWeeks; | |
} | |
static Request parse(String requestText) { | |
String[] segments = requestText.split("_"); | |
return new Request(Integer.parseInt(segments[0]), | |
segments[1].chars().map(i -> i - '0').toArray()); | |
} | |
public int getEmployee() { | |
return employee; | |
} | |
public int[] getDayOfWeeks() { | |
return dayOfWeeks; | |
} | |
} | |
static class Tuple<S, T> { | |
private S first; | |
private T second; | |
private Tuple(S first, T second) { | |
this.first = first; | |
this.second = second; | |
} | |
public static <S, T> Tuple<S, T> of(S first, T second) { | |
return new Tuple<>(first, second); | |
} | |
public S first() { | |
return first; | |
} | |
public T second() { | |
return second; | |
} | |
} | |
static final int MONDAY = 1; | |
static final int FRIDAY = 5; | |
static final int MAX_REQUEST = 5; | |
static final int MAX_EMPLOYEE = 4; | |
public String solve(String src) { | |
List<Request> requests = Arrays.stream(src.split("\\|")) | |
.map(s -> Request.parse(s)) | |
.collect(Collectors.toList()); | |
List<LessonClass> lessonClasses = IntStream.rangeClosed(MONDAY, FRIDAY) | |
.mapToObj(LessonClass::new) | |
.collect(Collectors.toList()); | |
IntStream.range(0, MAX_REQUEST).forEach(i -> { | |
List<Request> removeRequests = new ArrayList<>(); | |
requests.stream() | |
.map(r -> Tuple.of(lessonClasses.get(r.getDayOfWeeks()[i] - MONDAY), r)) | |
.filter(tuple -> tuple.first().employees.size() < MAX_EMPLOYEE) | |
.forEach(tuple -> { | |
tuple.first().employees.add(tuple.second().getEmployee()); | |
removeRequests.add(tuple.second()); | |
}); | |
removeRequests.forEach(r -> requests.remove(r)); | |
}); | |
return lessonClasses.stream() | |
.filter(lc -> !lc.getEmployees().isEmpty()) | |
.map(lc -> { | |
return lc.dayOfWeek | |
+ "_" | |
+ lc.getEmployees() | |
.stream() | |
.sorted() | |
.map(String::valueOf) | |
.collect(Collectors.joining(":")); | |
}) | |
.collect(Collectors.joining("|")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment