-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathMatch.java
More file actions
61 lines (49 loc) · 1.91 KB
/
Match.java
File metadata and controls
61 lines (49 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package nextstep.ladder.domain;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Match {
private static final int BEGIN_IDX = 0;
private final InputOutput inputOutput;
private final Ladder ladder;
public Match(InputOutput inputOutput, Ladder ladder) {
this.inputOutput = inputOutput;
this.ladder = ladder;
}
public Match(Ladder ladder) {
this(null, ladder);
}
public Result makeResult() {
List<Person> people = inputOutput.people().value();
List<ExecuteResult> executeResults = inputOutput.executeResults().value();
return new Result(IntStream.range(BEGIN_IDX, people.size())
.boxed()
.collect(Collectors.toMap(i -> people.get(i).name(), i -> executeResults.get(findOutputIdx(i)).name(),
(v1, v2) -> v1, LinkedHashMap::new)));
}
int findOutputIdx(int startIdx) {
int currentIdx = startIdx * 2;
String[][] ladder = this.ladder.result();
int rowCount = ladder.length;
int columnLength = ladder[BEGIN_IDX].length;
currentIdx = IntStream.range(BEGIN_IDX, rowCount)
.reduce(currentIdx, (idx, i) -> {
String[] row = ladder[i];
if (isMovableToLeft(idx, row)) {
return idx - 2;
}
if (isMovableToRight(idx, row, columnLength)) {
return idx + 2;
}
return idx;
});
return currentIdx / 2;
}
private boolean isMovableToLeft(int idx, String[] row) {
return idx > BEGIN_IDX && "h".equals(row[idx - 1]);
}
private boolean isMovableToRight(int idx, String[] row, int columnLength) {
return idx < columnLength - 1 && "h".equals(row[idx + 1]);
}
}