Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 31 additions & 63 deletions src/main/java/org/collins/FindPathApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import org.collins.model.Point;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -11,80 +15,44 @@ public class FindPathApplication {
static PathFinder pathFinder = new PathFinder();

public static void main(String[] args) {
// get filename from the args
if (args.length < 1)
return;
String filename = args[0];
List<String> grid = null;

try {
// read grid
grid = Files.readAllLines(new File(filename).toPath(), Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}

List<String> plane1 = new ArrayList<>();
plane1.add(" ");
plane1.add(" ");
plane1.add(" ");
plane1.add(" #########");
plane1.add(" ");

List<String> plane2 = new ArrayList<>();
plane2.add(" # ");
plane2.add(" # ");
plane2.add(" # ");
plane2.add(" ");

List<String> plane3 = new ArrayList<>();
plane3.add(" # ");
plane3.add(" # #");
plane3.add(" #");
plane3.add(" ");
plane3.add("# ##");
plane3.add(" ");

List<String> plane4 = new ArrayList<>();
plane4.add(" # ");
plane4.add("# #");
plane4.add("## #");
plane4.add(" ");
plane4.add("## #");
plane4.add("# ");

List<String> plane5 = new ArrayList<>();
plane5.add(" # ");
plane5.add(" # ");
plane5.add(" # ");
plane5.add(" ");
plane5.add("# ");

List<String> plane6 = new ArrayList<>();
plane6.add("____#");
plane6.add("###_#");
plane6.add("____#");
plane6.add("____#");
plane6.add("_____");

testPlane(plane1);
System.out.println("----------------------------------------");
testPlane(plane2);
System.out.println("----------------------------------------");
testPlane(plane3);
System.out.println("----------------------------------------");
testPlane(plane4);
System.out.println("----------------------------------------");
testPlane(plane5);
System.out.println("----------------------------------------");
testPlane(plane6);
System.out.println("----------------------------------------");
if (grid.isEmpty()){
System.out.printf("grid is empty: %s%n", filename);
return;
}
testGrid(grid);
System.out.println("_".repeat(grid.get(0).length()*2));
}

static void testPlane(List<String> plane) {
List<Point> result = pathFinder.findPath(plane);
static void testGrid(List<String> grid) {
List<Point> result = pathFinder.findPath(grid);
if (result != null) {
List<String> newPlane = printResultsPlane(plane, result);
newPlane
List<String> newGrid = printResultsGrid(grid, result);
newGrid
.forEach(row -> System.out.printf("|%s|\n", row));
}
}

static List<String> printResultsPlane(List<String> plane, List<Point> results) {
static List<String> printResultsGrid(List<String> grid, List<Point> results) {
System.out.printf("%d steps.\n", results.size());
for (Point point : results) {
StringBuilder sb = new StringBuilder(plane.get(point.getY()));
StringBuilder sb = new StringBuilder(grid.get(point.getY()));
sb.setCharAt(point.getX(), '⭕');
plane.set(point.getY(), sb.toString());
grid.set(point.getY(), sb.toString());
}
return plane.stream()
return grid.stream()
.map(s -> s.replace(" ", "\uD83D\uDD34"))
.map(s -> s.replace("_", "\uD83D\uDD34"))
.map(s -> s.replace("#", "\uD83D\uDFE5"))
Expand Down
49 changes: 29 additions & 20 deletions src/main/java/org/collins/PathFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@

public class PathFinder {

public List<Point> findPath(List<String> plane) {
if (plane.isEmpty())
public List<Point> findPath(List<String> grid) {
if (grid.isEmpty())
return null;

if (!isPlaneRectangular(plane)){
System.out.println("bad plane: all rows not same length");
if (!isGridRectangular(grid)){
System.out.println("bad grid: all rows not same length");
return null;
}

Point start = new Point(0, 0);
Point end = new Point(plane.get(0).length() - 1, plane.size() - 1);
Point end = new Point(grid.get(0).length() - 1, grid.size() - 1);

return _findPath(plane, start, end, List.of());
return _findPath(grid, start, end, List.of());
}

private List<Point> _findPath(List<String> plane, Point start, Point end, List<Point> path) {
private List<Point> _findPath(List<String> grid, Point start, Point end, List<Point> path) {
// base case
if (start.equals(end)) {
path.add(end);
return path;
}

// is this a dead end
if (plane.get(start.getY()).charAt(start.getX()) == '#') {
if (grid.get(start.getY()).charAt(start.getX()) == '#') {
return null;
}

Expand All @@ -42,25 +42,34 @@ private List<Point> _findPath(List<String> plane, Point start, Point end, List<P
Point right = new Point(start.getX() + 1, start.getY());
Point down = new Point(start.getX(), start.getY() + 1);

List<Point> downResult = null;
// can we go down
if (down.getY() < plane.size()) {
List<Point> downResult = _findPath(plane, down, end, newPath);
if (downResult != null) {
return downResult;
}
if (down.getY() < grid.size()) {
downResult = _findPath(grid, down, end, newPath);
}

List<Point> rightResult = null;
// can we go right
if (right.getX() < plane.get(0).length()) {
List<Point> rightResult = _findPath(plane, right, end, newPath);
if (right.getX() < grid.get(0).length()) {
rightResult = _findPath(grid, right, end, newPath);
}

if (rightResult == null){
return downResult;
}
if(downResult == null){
return rightResult;
}
return null;

if (downResult.size() < rightResult.size()){
return downResult;
}

return rightResult;
}

boolean isPlaneRectangular(List<String> plane) {
int firstLen = plane.get(0).length();
return plane.stream()
boolean isGridRectangular(List<String> grid) {
int firstLen = grid.get(0).length();
return grid.stream()
.allMatch(s -> s.length() == firstLen);
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/grid1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
___________________________
______###########__________
_#### #########__________
_____## #########__________
______# ########__________
______## #######__________
______### #######__________
______### #####__________
______##### #####__________
___________________________
___________________________
11 changes: 11 additions & 0 deletions src/main/resources/grid2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
______###########__________
______###########__________
#####_ #########__________
_____## #########__________
______# ########__________
______## #######__________
______### #######__________
______### #####__________
______##### #####__________
___________________________
___________________________