-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputParser.java
61 lines (55 loc) · 1.86 KB
/
InputParser.java
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
import java.io.*;
import java.util.*;
public class InputParser
{
private static int[] parseCoordinates(String line)
{
String[] commasplit = line.split(",");
if (commasplit == null || commasplit.length != 3)
{
return null;
}
int[] coords = new int[commasplit.length];
for (int i=0; i<commasplit.length; i++)
{
coords[i] = Integer.valueOf(commasplit[i]);
}
return coords;
}
public static Cube parseInput(String filePath) throws IOException
{
try (BufferedReader reader = new BufferedReader(new FileReader(filePath)))
{
String line;
int[] gridDimensions = null;
ArrayList<Bee> bees = new ArrayList<>();
ArrayList<int[]> hiveSlots = new ArrayList<>();
int hives = 0;
while ((line = reader.readLine()) != null)
{
line = line.trim();
int[] coordinates = parseCoordinates(line);
if (line.isEmpty() || coordinates == null) continue;
if (gridDimensions == null)
{
gridDimensions = coordinates;
}
else if (hiveSlots.size() < 15)
{
hiveSlots.add(coordinates);
}
else if (bees.size() < 15)
{
bees.add(new Bee((bees.size()+1),coordinates, hiveSlots.get(bees.size())));
}
else
{
break;
}
}
Cube cube = new Cube(gridDimensions, bees);
System.out.println("The hive is from" + Arrays.toString(hiveSlots.get(0)) + "to " + Arrays.toString(hiveSlots.get(hiveSlots.size()-1)));
return cube;
}
}
}