Skip to content

Commit 3b1bd20

Browse files
committed
Add coordinats(x,y) to each pizza cell
1 parent 57b15e9 commit 3b1bd20

File tree

6 files changed

+56
-30
lines changed

6 files changed

+56
-30
lines changed

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

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

3+
import com.google.hashcode.entity.Cell;
4+
import com.google.hashcode.entity.Ingredient;
35
import com.google.hashcode.entity.Pizza;
4-
import com.google.hashcode.entity.PizzaCell;
56
import com.google.hashcode.utils.IoUtils;
67

78
import java.io.File;
@@ -11,8 +12,8 @@
1112
public class App {
1213
public static void main(String[] args) throws IOException {
1314
String exampleInputFile = "inputDataSets/example.in";
14-
PizzaCell[][] pizzaCells = IoUtils.parsePizza(exampleInputFile);
15-
Pizza pizza = new Pizza(new File(exampleInputFile), pizzaCells, IoUtils.parseSliceInstructions(exampleInputFile));
15+
Cell[][] ingredients = IoUtils.parsePizza(exampleInputFile);
16+
Pizza pizza = new Pizza(new File(exampleInputFile), ingredients, IoUtils.parseSliceInstructions(exampleInputFile));
1617

1718
System.out.println("GoogleHashCode2017! Pizza task");
1819
System.out.print(pizza);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.google.hashcode.entity;
2+
3+
/**
4+
* Represents a pizza cell with it coordinates. There is no getters/setters for simplicity
5+
*
6+
* @author Grigoriy Lyashenko (Grog).
7+
*/
8+
public class Cell {
9+
final int x;
10+
final int y;
11+
final Ingredient ingredient;
12+
13+
public Cell(int x, int y, Ingredient ingredient) {
14+
this.x = x;
15+
this.y = y;
16+
this.ingredient = ingredient;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return ingredient.toString();
22+
}
23+
}

src/main/java/com/google/hashcode/entity/PizzaCell.java renamed to src/main/java/com/google/hashcode/entity/Ingredient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
*
66
* @author Grigoriy Lyashenko (Grog).
77
*/
8-
public enum PizzaCell {
8+
public enum Ingredient {
99
MUSHROOM("M"), TOMATO("T");
1010
private final String type;
1111

12-
PizzaCell(final String type) {
12+
Ingredient(final String type) {
1313
this.type = type;
1414
}
1515

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@
1212
public class Pizza {
1313

1414
private final File input;
15-
private final PizzaCell[][] pizzaCells;
15+
private final Cell[][] cells;
1616
private final SliceInstruction sliceInstruction;
1717

18-
public Pizza(File input, PizzaCell[][] pizzaCells, SliceInstruction sliceInstruction) {
18+
public Pizza(File input, Cell[][] cells, SliceInstruction sliceInstruction) {
1919
this.input = input;
20-
this.pizzaCells = pizzaCells;
20+
this.cells = cells;
2121
this.sliceInstruction = sliceInstruction;
2222
}
2323

2424
public File getInput() {
2525
return input;
2626
}
2727

28-
public PizzaCell[][] getPizzaCells() {
29-
return pizzaCells;
28+
public Cell[][] getCells() {
29+
return cells;
3030
}
3131

3232

3333
@Override
3434
public String toString() {
3535
return input.toString() +
36-
"\n" + IoUtils.convertToHumanReadableTable(pizzaCells) +
36+
"\n" + IoUtils.convertToHumanReadableTable(cells) +
3737
"\n" + sliceInstruction.toString();
3838
}
3939

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

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

3-
import com.google.hashcode.entity.PizzaCell;
3+
import com.google.hashcode.entity.Cell;
4+
import com.google.hashcode.entity.Ingredient;
45
import com.google.hashcode.entity.SliceInstruction;
56

67
import java.io.BufferedReader;
@@ -19,29 +20,29 @@ public class IoUtils {
1920
* @return 2d array representing a pizza
2021
* @throws IOException parsing fail
2122
*/
22-
public static PizzaCell[][] parsePizza(String file) throws IOException {
23+
public static Cell[][] parsePizza(String file) throws IOException {
2324
try (FileReader fileReader = new FileReader(file)) {
2425
BufferedReader br = new BufferedReader(fileReader);
2526
//parse the first line
2627
String[] headerTokens = br.readLine().split(" ");
2728
int rowsCount = Integer.parseInt(headerTokens[0]);
2829
int columnsCount = Integer.parseInt(headerTokens[1]);
2930
//declare a pizza cells array
30-
PizzaCell[][] pizzaCells = new PizzaCell[rowsCount][columnsCount];
31+
Cell[][] ingredients = new Cell[rowsCount][columnsCount];
3132
int row = 0;
3233
String fileLine;
3334
while ((fileLine = br.readLine()) != null) {
34-
for (int i = 0; i < fileLine.length(); i++) {
35-
Character literal = fileLine.charAt(i);
36-
if (literal.toString().equals(PizzaCell.TOMATO.toString())) {
37-
pizzaCells[row][i] = PizzaCell.TOMATO;
38-
} else if (literal.toString().equals(PizzaCell.MUSHROOM.toString())) {
39-
pizzaCells[row][i] = PizzaCell.MUSHROOM;
35+
for (int column = 0; column < fileLine.length(); column++) {
36+
Character literal = fileLine.charAt(column);
37+
if (literal.toString().equals(Ingredient.TOMATO.toString())) {
38+
ingredients[row][column] = new Cell(column, row, Ingredient.TOMATO);
39+
} else if (literal.toString().equals(Ingredient.MUSHROOM.toString())) {
40+
ingredients[row][column] = new Cell(column, row, Ingredient.MUSHROOM);
4041
}
4142
}
4243
row++;
4344
}
44-
return pizzaCells;
45+
return ingredients;
4546
}
4647
}
4748

@@ -66,13 +67,13 @@ public static SliceInstruction parseSliceInstructions(String file) throws IOExce
6667
/**
6768
* Converts given pizza cells 2d array to human readable string representation
6869
*
69-
* @param pizzaCells given array
70+
* @param ingredients given array
7071
* @return table like String representation
7172
*/
72-
public static String convertToHumanReadableTable(PizzaCell[][] pizzaCells) {
73+
public static String convertToHumanReadableTable(Cell[][] ingredients) {
7374
StringBuilder output = new StringBuilder();
74-
for (Enum[] row : pizzaCells) {
75-
for (Enum cell : row) {
75+
for (Cell[] row : ingredients) {
76+
for (Cell cell : row) {
7677
output.append(cell).append(" ");
7778
}
7879
output.append("\n");

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

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

3-
import com.google.hashcode.entity.PizzaCell;
3+
import com.google.hashcode.entity.Cell;
4+
import com.google.hashcode.entity.Ingredient;
45
import org.junit.Test;
56

67
import java.io.IOException;
@@ -16,10 +17,10 @@ public class IoUtilsTest {
1617

1718
@Test
1819
public void parseExampleInput() throws IOException {
19-
PizzaCell[][] pizzaCells = IoUtils.parsePizza(examplePizzaFile);
20-
assertEquals("We expect" + examplePizzaFile + "contains 3 rows", 3, pizzaCells.length);
21-
assertEquals("We expect" + examplePizzaFile + "contains 5 columns", 5, pizzaCells[0].length);
22-
assertFalse("We expect no null value in pizzaCells", IoUtils.convertToHumanReadableTable(pizzaCells).contains("null"));
20+
Cell[][] ingredients = IoUtils.parsePizza(examplePizzaFile);
21+
assertEquals("We expect" + examplePizzaFile + "contains 3 rows", 3, ingredients.length);
22+
assertEquals("We expect" + examplePizzaFile + "contains 5 columns", 5, ingredients[0].length);
23+
assertFalse("We expect no null value in ingredients", IoUtils.convertToHumanReadableTable(ingredients).contains("null"));
2324
}
2425

2526
@Test

0 commit comments

Comments
 (0)