Skip to content

Commit 1bb368b

Browse files
committed
Merge remote-tracking branch 'remotes/origin/simplestAlgorithm' into DFSmethods
2 parents 9ffef12 + c5626d5 commit 1bb368b

File tree

12 files changed

+342
-78
lines changed

12 files changed

+342
-78
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ To automate interaction with online submission can be used [SeleniumIDE](https:/
3131
with a firefox browser.
3232
* login to the [submission page](https://hashcodejudge.withgoogle.com/#/rounds/6553823069863936/submissions/)
3333
* setup selenium test suite(submitResultsViaSelenium) according to yours file system
34-
* execute the test case and see scores on web
34+
* execute the test case and see scores on web. See the [video instruction on YouTube](https://www.youtube.com/watch?v=Wg7s3CtIeCs&feature=youtu.be)
3535

3636

outputDataSet/example.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
3
2-
0 0 2 1
3-
0 2 2 2
4-
0 3 2 4
1+
2
2+
0 0 1 1
3+
0 2 1 3

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
<version>4.12</version>
2525
<scope>test</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.slf4j</groupId>
29+
<artifactId>slf4j-api</artifactId>
30+
<version>1.7.21</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>ch.qos.logback</groupId>
34+
<artifactId>logback-core</artifactId>
35+
<version>1.1.7</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>ch.qos.logback</groupId>
39+
<artifactId>logback-classic</artifactId>
40+
<version>1.1.7</version>
41+
</dependency>
2742
</dependencies>
2843

2944
</project>

src/main/java/com/google/hashcode/App.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
import com.google.hashcode.entity.Cell;
44
import com.google.hashcode.entity.Pizza;
5+
import com.google.hashcode.service.Slicer;
56
import com.google.hashcode.utils.IoUtils;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
69

710
import java.io.File;
811
import java.io.IOException;
12+
import java.util.List;
913

1014

1115
public class App {
16+
private static final Logger LOGGER = LoggerFactory.getLogger(Slicer.class);
17+
1218
public static void main(String[] args) throws IOException {
1319
String exampleInputFile = "inputDataSets/example.in";
14-
Cell[][] ingredients = IoUtils.parsePizza(exampleInputFile);
20+
List<Cell> ingredients = IoUtils.parsePizza(exampleInputFile);
1521
Pizza pizza = new Pizza(new File(exampleInputFile), ingredients, IoUtils.parseSliceInstructions(exampleInputFile));
16-
17-
System.out.println("GoogleHashCode2017! Pizza task");
18-
System.out.print(pizza);
22+
// IoUtils.writeToFile("outputDataSet/example.txt", IoUtils.parseSlices(Slicer.slicePizza(pizza)));
23+
LOGGER.info("GoogleHashCode2017! Pizza task");
24+
LOGGER.info(pizza.toString());
1925

2026
}
2127

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public class Cell {
99
public final int x;
1010
public final int y;
1111
public final Ingredient ingredient;
12+
/**
13+
* indicates if given cell has been sliced
14+
*/
15+
public boolean sliced = false;
1216

1317
public Cell(int x, int y, Ingredient ingredient) {
1418
this.x = x;
@@ -21,6 +25,37 @@ public String toString() {
2125
return ingredient.toString();
2226
}
2327

28+
/**
29+
* Creates a new cell based on the parent but add given coordinates
30+
* to the original one cell
31+
*
32+
* @param x delta x
33+
* @param y delta y
34+
* @return new Cell with adjusted coordinates
35+
*/
36+
public Cell prototype(int x, int y) {
37+
return new Cell(this.x + x, this.y + y, null);
38+
}
39+
40+
@Override
41+
public boolean equals(Object o) {
42+
if (this == o) return true;
43+
if (!(o instanceof Cell)) return false;
44+
45+
Cell cell = (Cell) o;
46+
47+
if (x != cell.x) return false;
48+
if (y != cell.y) return false;
49+
return true;
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
int result = x;
55+
result = 31 * result + y;
56+
return result;
57+
}
58+
2459
public int getX() {
2560
return x;
2661
}

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.google.hashcode.utils.IoUtils;
44

55
import java.io.File;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
69

710
/**
811
* Represents an immutable pizza
@@ -12,33 +15,34 @@
1215
public class Pizza {
1316

1417
private final File input;
15-
private final Cell[][] cells;
16-
private final SliceInstruction sliceInstruction;
17-
18-
public Pizza(File input, Cell[][] cells, SliceInstruction sliceInstruction) {
19-
this.input = input;
20-
this.cells = cells;
21-
this.sliceInstruction = sliceInstruction;
22-
}
18+
private final List<Cell> cells;
2319

2420
public File getInput() {
2521
return input;
2622
}
2723

28-
public Cell[][] getCells() {
24+
public List<Cell> getCells() {
2925
return cells;
3026
}
3127

28+
public SliceInstruction getSliceInstruction() {
29+
return sliceInstruction;
30+
}
31+
32+
private final SliceInstruction sliceInstruction;
33+
34+
public Pizza(File input, List<Cell> cells, SliceInstruction sliceInstruction) {
35+
this.input = input;
36+
this.cells = cells;
37+
this.sliceInstruction = sliceInstruction;
38+
}
3239

3340
@Override
3441
public String toString() {
3542
return input.toString() +
36-
"\n" + IoUtils.convertToHumanReadableTable(cells) +
43+
//TODO fix human readable output "\n" + IoUtils.convertToHumanReadableTable(cells) +
3744
"\n" + sliceInstruction.toString();
3845
}
3946

4047

41-
public SliceInstruction getSliceInstruction() {
42-
return sliceInstruction;
43-
}
4448
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.google.hashcode.entity;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A rectangle piece of a pizza
8+
*
9+
* @author Grigoriy Lyashenko (Grog).
10+
*/
11+
public class Slice {
12+
13+
public List<Cell> cells = new ArrayList<>();
14+
15+
public void add(Cell cell) {
16+
cells.add(cell);
17+
cell.sliced = true;
18+
}
19+
20+
public int cellsNumber() {
21+
return cells.size();
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return cells.toString();
27+
}
28+
}
29+
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.google.hashcode.service;
2+
3+
import com.google.hashcode.entity.*;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.Arrays;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
import static java.lang.Integer.valueOf;
13+
14+
/**
15+
* Slice a pizza according to a slice instructions
16+
*
17+
* @author Grigoriy Lyashenko (Grog).
18+
*/
19+
public class Slicer {
20+
/* private static final Logger LOGGER = LoggerFactory.getLogger(Slicer.class);
21+
22+
private Slicer() {
23+
}
24+
25+
public static List<Slice> slicePizza(Pizza pizza) {
26+
List<Cell> pizzaCells = pizza.getCells();
27+
List<Slice> pizzaSlices = new LinkedList<>();
28+
while (canBeSliced(pizza)) {
29+
Slice slice = step(pizzaCells);
30+
if (validateSlice(slice, pizza.getSliceInstruction())) {
31+
pizzaSlices.add(slice);
32+
}
33+
}
34+
LOGGER.info("slices:" + pizzaSlices);
35+
LOGGER.info("pizza slicing finished" +
36+
"\ntotal cells:" + pizza.cellsNumber() +
37+
"\nsliced cells :" + pizzaSlices.stream()
38+
.map(Slice::cellsNumber)
39+
.reduce(Integer::sum)
40+
.get() +
41+
"\nunsliced cells :" + pizza.unslicedCellsNumber());
42+
return pizzaSlices;
43+
}
44+
45+
46+
private static Slice step(List<Cell> pizzaCells) {
47+
Slice slice = new Slice();
48+
//find the top-left unused cell
49+
Cell startPosition = findStartPosition(pizzaCells);
50+
LOGGER.info("\nstart position: "
51+
+ "\ny: " + startPosition.y
52+
+ "\nx: " + startPosition.x);
53+
slice.add(pizzaCells[startPosition.y][startPosition.x]);
54+
slice.add(pizzaCells[startPosition.y][valueOf(startPosition.x) + 1]);
55+
slice.add(pizzaCells[valueOf(startPosition.y) + 1][startPosition.x]);
56+
slice.add(pizzaCells[valueOf(startPosition.y) + 1][valueOf(startPosition.x) + 1]);
57+
return slice;
58+
}
59+
60+
private static Cell findStartPosition(List<Cell> pizzaCells) {
61+
for (int row = 0; row < pizzaCells.size(); row++) {
62+
Cell[] pizzaRow = pizzaCells[row];
63+
for (int column = 0; column < pizzaRow.length; column++) {
64+
Cell cell = pizzaRow[column];
65+
if (!cell.sliced) {
66+
//get it as a start
67+
return cell;
68+
}
69+
}
70+
}
71+
//default return
72+
return pizzaCells[0][0];
73+
}
74+
75+
static boolean validateSlice(Slice slice, SliceInstruction sliceInstruction) {
76+
int mushroomsNumber = slice.cells.stream()
77+
.filter(cell -> cell.ingredient.equals(Ingredient.MUSHROOM))
78+
.collect(Collectors.toList())
79+
.size();
80+
int tomatoesNumber = slice.cells.stream()
81+
.filter(cell -> cell.ingredient.equals(Ingredient.TOMATO))
82+
.collect(Collectors.toList())
83+
.size();
84+
boolean passSliceInstructions = slice.cells.size() <= sliceInstruction.getMaxNumberOfCellsPerSlice()
85+
&& tomatoesNumber >= sliceInstruction.getMinNumberOfIngredientPerSlice()
86+
&& mushroomsNumber >= sliceInstruction.getMinNumberOfIngredientPerSlice();
87+
LOGGER.info("\n" + sliceInstruction +
88+
"\nSlice :" + slice +
89+
"\npass validation: " + passSliceInstructions);
90+
return passSliceInstructions;
91+
}
92+
93+
static boolean canBeSliced(Pizza pizza) {
94+
final List<Cell> unusedCells = Arrays.stream(pizza.getCells())
95+
.flatMap(Arrays::stream)
96+
.filter(cell -> !cell.sliced)
97+
.collect(Collectors.toList());
98+
int mushroomsNumber = unusedCells.stream()
99+
.filter(cell -> cell.ingredient.equals(Ingredient.MUSHROOM))
100+
.collect(Collectors.toList())
101+
.size();
102+
int tomatoesNumber = unusedCells.stream()
103+
.filter(cell -> cell.ingredient.equals(Ingredient.TOMATO))
104+
.collect(Collectors.toList())
105+
.size();
106+
SliceInstruction sliceInstruction = pizza.getSliceInstruction();
107+
boolean canBeSliced = unusedCells.size() > sliceInstruction.getMinNumberOfIngredientPerSlice() * 2
108+
&& tomatoesNumber > sliceInstruction.getMinNumberOfIngredientPerSlice()
109+
&& mushroomsNumber > sliceInstruction.getMinNumberOfIngredientPerSlice();
110+
LOGGER.info(
111+
"\ncells suitable for slicing :" + unusedCells.size() +
112+
"\n :" + unusedCells +
113+
"\ncan be sliced: " + canBeSliced);
114+
return canBeSliced;
115+
}*/
116+
}

0 commit comments

Comments
 (0)