Skip to content

Commit 931105b

Browse files
committed
feat: BridgesGeneratorByCreatingCandidates
모든 Line 후보를 만들어놓고 shuffle을 통해 하나의 후보를 꺼내쓰는 전략
1 parent e954912 commit 931105b

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package nextstep.ladder.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
public class BridgesGeneratorByCreatingCandidates implements BridgesGenerator {
10+
11+
private static List<List<Bridge>> candidates = new ArrayList<>();
12+
13+
static void generateLadders(int numbersOfPeople) {
14+
List<List<Bridge>> result = new ArrayList<>(List.of(List.of(Bridge.of(false)), List.of(Bridge.of(true))));
15+
16+
for (int i = 2; i < numbersOfPeople ; i++) {
17+
result = dp(result);
18+
}
19+
20+
candidates = result;
21+
}
22+
23+
static List<List<Bridge>> dp(List<List<Bridge>> result) {
24+
return result.stream()
25+
.flatMap(ladder -> {
26+
List<Bridge> withFalse = new ArrayList<>(ladder);
27+
withFalse.add(Bridge.of(false));
28+
List<Bridge> withTrue = new ArrayList<>(ladder);
29+
if (!ladder.get(ladder.size() - 1).canCrossBridge()) {
30+
withTrue.add(Bridge.of(true));
31+
return Stream.of(withFalse, withTrue);
32+
}
33+
return Stream.of(withFalse);
34+
})
35+
.collect(Collectors.toList());
36+
}
37+
38+
@Override
39+
public List<Bridge> generateBridges(Integer numbersOfPeople) {
40+
if (candidates.isEmpty()) {
41+
generateLadders(numbersOfPeople);
42+
}
43+
Collections.shuffle(candidates);
44+
return candidates.get(0);
45+
}
46+
47+
48+
49+
}

src/main/java/nextstep/ladder/model/SequentialRandomBridgesGenerator.java src/main/java/nextstep/ladder/model/BridgesGeneratorBySequentialRandom.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.List;
55
import java.util.concurrent.ThreadLocalRandom;
66

7-
public class SequentialRandomBridgesGenerator implements BridgesGenerator {
7+
public class BridgesGeneratorBySequentialRandom implements BridgesGenerator {
88

99
@Override
1010
public List<Bridge> generateBridges(Integer numbersOfPeople) {

src/main/java/nextstep/ladder/model/LinesGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class LinesGenerator {
88

9-
private final BridgesGenerator bridgesGenerator = new SequentialRandomBridgesGenerator();
9+
private final BridgesGenerator bridgesGenerator = new BridgesGeneratorBySequentialRandom();
1010

1111
public List<Line> generatorLines(Integer numbersOfPeople, Integer height) {
1212
return IntStream.range(0, height)

src/test/java/nextstep/ladder/model/SequentialRandomBridgesGeneratorTest.java src/test/java/nextstep/ladder/model/BridgesGeneratorBySequentialRandomTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import org.junit.jupiter.api.Test;
66

7-
public class SequentialRandomBridgesGeneratorTest {
7+
public class BridgesGeneratorBySequentialRandomTest {
88

9-
private final BridgesGenerator bridgesGenerator = new SequentialRandomBridgesGenerator();
9+
private final BridgesGenerator bridgesGenerator = new BridgesGeneratorBySequentialRandom();
1010

1111
@Test
1212
public void generateBridges_다리_개수_확인() {

0 commit comments

Comments
 (0)