-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathIoUtils.java
More file actions
116 lines (107 loc) · 4.12 KB
/
Copy pathIoUtils.java
File metadata and controls
116 lines (107 loc) · 4.12 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.google.hashcode.utils;
import com.google.hashcode.entity.Cell;
import com.google.hashcode.entity.Ingredient;
import com.google.hashcode.entity.Slice;
import com.google.hashcode.entity.SliceInstruction;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Formatter;
import java.util.List;
/**
* @author Grigoriy Lyashenko (Grog).
* @author github.com/VadimKlindukhov skype: kv_vadim
*/
public class IoUtils {
private IoUtils() {
}
/**
* Parses given input file to a 2d pizza cells array
*
* @param file input file
* @return 2d array representing a pizza
* @throws IOException parsing fail
*/
public static List<Cell> parsePizza(String file) throws IOException {
try (FileReader fileReader = new FileReader(file)) {
BufferedReader br = new BufferedReader(fileReader);
//skip a line with slice instructions
br.readLine();
//declare a pizza cells array
List<Cell> cells = new ArrayList<>();
int row = 0;
String fileLine;
while ((fileLine = br.readLine()) != null) {
for (int column = 0; column < fileLine.length(); column++) {
Character literal = fileLine.charAt(column);
if (literal.toString().equals(Ingredient.TOMATO.toString())) {
cells.add(new Cell(row, column, Ingredient.TOMATO));
} else if (literal.toString().equals(Ingredient.MUSHROOM.toString())) {
cells.add(new Cell(row, column, Ingredient.MUSHROOM));
}
}
row++;
}
return cells;
}
}
/**
* Produces SliceInstructions based on given input data set
*
* @param file input data set
* @return slice instructions
* @throws IOException file reading error
*/
public static SliceInstruction parseSliceInstructions(String file) throws IOException {
try (FileReader fileReader = new FileReader(file)) {
BufferedReader br = new BufferedReader(fileReader);
String[] headerTokens = br.readLine().split(" ");
int minNumberOfIngredientPerSlice = Integer.parseInt(headerTokens[2]);
int maxNumberOfCellsPerSlice = Integer.parseInt(headerTokens[3]);
return new SliceInstruction(minNumberOfIngredientPerSlice, maxNumberOfCellsPerSlice);
}
}
/**
* Formats data from list of slices to the required output format
*
* @param list inner representation of pizza
* @return String that contains output data
*/
public static String parseSlices(List<Slice> list) {
Comparator<Cell> cellComparator = (Cell c1, Cell c2) -> {
if (c1.x != c2.x) {
return Integer.compare(c1.x, c2.x);
} else
return Integer.compare(c1.y, c2.y);
};
StringBuilder sb = new StringBuilder();
Formatter textFormatter = new Formatter(sb);
textFormatter.format("%d%n", list.size());
Cell min, max;
for (Slice slice : list) {
min = slice.cells.stream().min(cellComparator).get();
max = slice.cells.stream().max(cellComparator).get();
textFormatter.format("%d %d %d %d%n", min.y, min.x, max.y, max.x);
}
textFormatter.close();
return sb.toString().trim();
}
public static void writeToFile(String fileName, String outputDate) throws IOException {
try (PrintWriter out = new PrintWriter(fileName)) {
out.println(outputDate);
}
}
public static String readFromFile(String fileName) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(fileName));
StringBuilder stringBuilder = new StringBuilder();
lines.forEach(
line -> stringBuilder.append(line).append("\n")
);
return stringBuilder.toString();
}
}