2018-01-24 02:56:52 -05:00
|
|
|
/* Reads a file of coordinates
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Testing:
|
|
|
|
* - Only valid coordinates are added (through regex)
|
|
|
|
* -
|
|
|
|
*/
|
2018-01-23 21:50:06 -05:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
public class Mouse {
|
|
|
|
|
|
|
|
private ArrayList<MousePath> mousePaths;
|
|
|
|
|
|
|
|
public Mouse(String path) {
|
|
|
|
mousePaths = new ArrayList<MousePath>();
|
|
|
|
readFile(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void readFile(String path) {
|
|
|
|
try {
|
|
|
|
File file = new File(path);
|
|
|
|
FileReader fileReader = new FileReader(file);
|
|
|
|
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
2018-01-24 02:56:52 -05:00
|
|
|
Pattern linePattern = Pattern.compile("[0-9]*,[0-9]*,[0-9]*$");
|
2018-01-23 21:50:06 -05:00
|
|
|
|
|
|
|
String line;
|
|
|
|
Point lastPoint = new Point(0, 0, 0);
|
|
|
|
int numberOfRepeats = 0;
|
|
|
|
ArrayList<Point> currentPath = new ArrayList<Point>();
|
|
|
|
currentPath.add(lastPoint);
|
2018-01-24 02:56:52 -05:00
|
|
|
|
2018-01-23 21:50:06 -05:00
|
|
|
while ((line = bufferedReader.readLine()) != null) {
|
|
|
|
if (!isLineValid(line, linePattern)) {
|
|
|
|
System.out.println(line + " does not match regex -- SKIPPING");
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-24 02:56:52 -05:00
|
|
|
|
2018-01-23 21:50:06 -05:00
|
|
|
Point point = getPointFromLine(line);
|
2018-01-24 02:56:52 -05:00
|
|
|
if (!point.isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-23 21:50:06 -05:00
|
|
|
|
2018-01-24 02:56:52 -05:00
|
|
|
if (point.hasSameLocation(lastPoint)) {
|
|
|
|
numberOfRepeats++;
|
2018-01-23 21:50:06 -05:00
|
|
|
if (numberOfRepeats == 20) {
|
2018-01-24 02:56:52 -05:00
|
|
|
if (currentPath.size() < 5) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-23 21:50:06 -05:00
|
|
|
MousePath newPath = new MousePath(currentPath);
|
2018-01-24 02:56:52 -05:00
|
|
|
mousePaths.add(newPath); // Deep copies
|
2018-01-23 21:50:06 -05:00
|
|
|
currentPath.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
numberOfRepeats = 0;
|
|
|
|
currentPath.add(point);
|
|
|
|
}
|
|
|
|
lastPoint = point;
|
|
|
|
}
|
|
|
|
fileReader.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isLineValid(String line, Pattern linePattern) {
|
|
|
|
Matcher matcher = linePattern.matcher(line);
|
|
|
|
if (matcher.find()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Point getPointFromLine(String line) {
|
2018-01-24 02:56:52 -05:00
|
|
|
String[] parts = line.split(Pattern.quote(","));
|
|
|
|
return new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
|
2018-01-23 21:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void displayPaths() {
|
|
|
|
for (MousePath path : mousePaths) {
|
|
|
|
path.display();
|
|
|
|
System.out.println("----------------------------------------------------------");
|
|
|
|
}
|
2018-01-24 02:56:52 -05:00
|
|
|
System.out.println("There are " + mousePaths.size() + " paths.");
|
2018-01-23 21:50:06 -05:00
|
|
|
}
|
|
|
|
}
|