Skip to content

Commit 2011e29

Browse files
committed
DFS_performStep. Implement pick up a slices list with minimal total cells number
1 parent 37b4783 commit 2011e29

File tree

5 files changed

+72
-28
lines changed

5 files changed

+72
-28
lines changed

src/main/java/com/google/hashcode/entity/Slice.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
public class Slice {
1515
private static final Logger LOGGER = LoggerFactory.getLogger(Slice.class);
16-
1716
public List<Cell> cells = new ArrayList<>();
1817

1918
public Slice() {
@@ -27,6 +26,10 @@ public Slice(List<Cell> cells) {
2726
this.cells = cells;
2827
}
2928

29+
public List<Cell> getCells() {
30+
return cells;
31+
}
32+
3033
@Override
3134
public boolean equals(Object o) {
3235
if (this == o) return true;
@@ -110,7 +113,7 @@ public Slice generateStepDeltaBelow() {
110113
public Slice generateStepDeltaLeft() {
111114
List<Cell> delta = new ArrayList<>();
112115
for (int y = this.minY(); y <= this.maxY(); y++) {
113-
Cell cell = new Cell(y, minX() -1 , Ingredient.TOMATO);
116+
Cell cell = new Cell(y, minX() - 1, Ingredient.TOMATO);
114117
delta.add(cell);
115118
}
116119
LOGGER.info("generateStepDeltaLeft"

src/main/java/com/google/hashcode/entity/Step.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@
88
*/
99
public class Step {
1010

11-
public Step(Slice startPosition, Slice delta) {
12-
super();
13-
this.startPosition = startPosition;
14-
this.delta = delta;
15-
}
16-
17-
public Slice startPosition;
18-
11+
public Slice startPosition;
1912
public Slice delta;
2013

21-
@Override
22-
public String toString() {
23-
return "Step{" +
24-
"startPosition=" + startPosition +
25-
", delta=" + delta +
26-
'}';
27-
}
14+
public Step(Slice startPosition, Slice delta) {
15+
super();
16+
this.startPosition = startPosition;
17+
this.delta = delta;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "Step{" +
23+
"startPosition=" + startPosition +
24+
", delta=" + delta +
25+
'}';
26+
}
2827

2928
}

src/main/java/com/google/hashcode/utils/DFSMethods.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
3535
.map(slicesRightBorderCell -> pizza.getCell(slicesRightBorderCell.y, slicesRightBorderCell.x + 1))
3636
.collect(Collectors.toList()));
3737
//check is step is valid
38-
Slice sliceAndStep = new Slice(new ArrayList<>(slice.cells));
38+
Slice sliceAndStep = new Slice(new ArrayList(slice.cells));
3939
sliceAndStep.cells.addAll(step.cells);
4040
if (!slice.cells.isEmpty() && sliceAndStep.isValid(pizza)) {
4141
//remove the slice and step from the pizza
@@ -79,9 +79,18 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
7979
return groupedSteps;
8080
}
8181

82-
public static Slice performStep(Pizza pizza, List<Step> steps) {
83-
//TODO pick-ups a step with a minimal steps number, execute it(cut it from the pizza, and a slice)
84-
return null;
82+
/**
83+
* Pick-ups a step with a minimal cells delta number,
84+
* execute it(cut it from the pizza, and add to a slice)
85+
*
86+
* @param pizza given pizza
87+
* @param steps available steps
88+
* @return formed slice that includes an original slice and delta from a step
89+
*/
90+
public static List<Step> performStep(Pizza pizza, Map<Slice, List<Step>> steps) {
91+
//1. Pick ups a steps list with minimal total cells number
92+
final Optional<List<Step>> minStep = steps.values().stream().min((o1, o2) -> new StepsComparator().compare(o1, o2));
93+
return minStep.get();
8594
}
8695

8796
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.google.hashcode.utils;
2+
3+
import com.google.hashcode.entity.Step;
4+
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
/**
9+
* @author Grigoriy Lyashenko (Grog).
10+
*/
11+
public class StepsComparator implements Comparator<List<Step>> {
12+
13+
@Override
14+
public int compare(List<Step> o1, List<Step> o2) {
15+
long o1CellsCount = o1.stream()
16+
.flatMap(step -> step.delta.cells.stream())
17+
.count();
18+
long o2CellsCount = o2.stream()
19+
.flatMap(step -> step.delta.cells.stream())
20+
.count();
21+
return Long.compare(o1CellsCount, o2CellsCount);
22+
}
23+
}

src/test/java/com/google/hashcode/utils/DFSMethodsTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.google.hashcode.utils;
22

33
import com.google.hashcode.entity.*;
4+
import org.junit.Before;
45
import org.junit.Test;
56

67
import java.io.File;
@@ -18,10 +19,15 @@
1819
*/
1920
public class DFSMethodsTest {
2021

22+
private Pizza pizza;
23+
24+
@Before
25+
public void setup() throws IOException {
26+
pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
27+
}
28+
2129
@Test
2230
public void rightStep() throws Exception {
23-
//Given a pizza
24-
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
2531
//then perform a step right from a particular cell
2632
Slice actualSlice = DFSMethods.rightStep(pizza, new Slice(pizza.getCell(1, 3))).get();
2733
Slice expectedSlice = new Slice(Arrays.asList(new Cell(1, 3, Ingredient.MUSHROOM), new Cell(1, 4, Ingredient.TOMATO)));
@@ -34,17 +40,15 @@ public void rightStep() throws Exception {
3440

3541
@Test
3642
public void getAvailableSteps() throws IOException {
37-
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
3843
Map<Slice, List<Step>> actualMap = DFSMethods.getAvailableSteps(pizza, DFSMethods.cutAllStartPositions(pizza));
3944
assertEquals(3, actualMap.keySet().size());
40-
assertEquals(3, actualMap.get(new Slice(new Cell(1,1, Ingredient.MUSHROOM))).size());
41-
assertEquals(2, actualMap.get(new Slice(new Cell(1,2, Ingredient.MUSHROOM))).size());
42-
assertEquals(3, actualMap.get(new Slice(new Cell(1,3, Ingredient.MUSHROOM))).size());
45+
assertEquals(3, actualMap.get(new Slice(new Cell(1, 1, Ingredient.MUSHROOM))).size());
46+
assertEquals(2, actualMap.get(new Slice(new Cell(1, 2, Ingredient.MUSHROOM))).size());
47+
assertEquals(3, actualMap.get(new Slice(new Cell(1, 3, Ingredient.MUSHROOM))).size());
4348
}
4449

4550
@Test
4651
public void cutAllStartPositions() throws IOException {
47-
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
4852
List<Slice> expected = Arrays.asList(
4953
new Slice(new Cell(1, 1, Ingredient.MUSHROOM)),
5054
new Slice(new Cell(1, 2, Ingredient.MUSHROOM)),
@@ -53,4 +57,10 @@ public void cutAllStartPositions() throws IOException {
5357
assertEquals(expected, DFSMethods.cutAllStartPositions(pizza));
5458
}
5559

60+
@Test
61+
public void performStep() {
62+
//TODO finish the method implementation
63+
List<Step> minSteps = DFSMethods.performStep(pizza, DFSMethods.getAvailableSteps(pizza, DFSMethods.cutAllStartPositions(pizza)));
64+
System.err.println(minSteps);
65+
}
5666
}

0 commit comments

Comments
 (0)