Skip to content

Commit aa4169c

Browse files
committed
Runbot
1 parent 5cc71d1 commit aa4169c

File tree

6 files changed

+100
-17
lines changed

6 files changed

+100
-17
lines changed

src/main/java/com/sbaars/adventofcode2019/common/Direction.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ public Point move(Point currentLocation) {
3434
case EAST: return new Point(currentLocation.x+1, currentLocation.y);
3535
case WEST: return new Point(currentLocation.x-1, currentLocation.y);
3636
}
37-
return null;
37+
throw new IllegalStateException("Non-existent Direction: "+this);
38+
}
39+
40+
public Direction opposite() {
41+
switch (this) {
42+
case NORTH: return SOUTH;
43+
case SOUTH: return NORTH;
44+
case EAST: return WEST;
45+
case WEST: return EAST;
46+
}
47+
throw new IllegalStateException("Non-existent Direction: "+this);
48+
}
49+
50+
public static Direction getByMove(Point from, Point to) {
51+
if(to.x > from.x) return EAST;
52+
else if(to.x < from.x) return WEST;
53+
else if(to.y > from.y) return SOUTH;
54+
else if(to.y < from.y) return NORTH;
55+
throw new IllegalStateException("From and to location are the same: "+from+", "+to);
3856
}
3957
}

src/main/java/com/sbaars/adventofcode2019/days/Day15.java

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@
99
import com.sbaars.adventofcode2019.common.Day;
1010
import com.sbaars.adventofcode2019.common.Direction;
1111
import com.sbaars.adventofcode2019.intcode.IntcodeComputer;
12+
import com.sbaars.adventofcode2019.pathfinding.Grid2d;
1213

1314
public class Day15 implements Day {
15+
16+
private static final int UNEXPLORED = -1;
17+
private static final int WALL = 0;
18+
private static final int PATH = 1;
19+
private static final int FINISH = 2;
20+
private static final int BOARD_SIZE = 100;
21+
private static final Point START_POINT = new Point(BOARD_SIZE/2,BOARD_SIZE/2);
1422

1523
public static void main(String[] args) throws IOException {
1624
new Day15().printParts();
@@ -19,14 +27,68 @@ public static void main(String[] args) throws IOException {
1927
@Override
2028
public Object part1() throws IOException {
2129
IntcodeComputer ic = new IntcodeComputer(15);
22-
List<Point> walls = new ArrayList<>();
23-
Point pos = new Point(50,50);
24-
int[][] grid = new int[100][100];
25-
for(int[] row : grid) Arrays.fill(row, -1);
30+
Point pos = START_POINT;
31+
int[][] grid = new int[BOARD_SIZE][BOARD_SIZE];
32+
for(int[] row : grid) Arrays.fill(row, UNEXPLORED);
2633
grid[pos.y][pos.x] = 0;
27-
Direction dir = Direction.NORTH;
2834
while(true) {
29-
ic.run();
35+
explore(grid, pos, ic);
36+
pos = moveToUnexploredPlace(grid, pos, ic);
37+
if(pos == null) {
38+
Grid2d map2d = new Grid2d(grid, false);
39+
System.out.println(Arrays.deepToString(grid));
40+
return map2d.findPath(START_POINT, findPos(grid, FINISH).get(0)).length;
41+
}
42+
}
43+
}
44+
45+
private Point moveToUnexploredPlace(int[][] grid, Point pos, IntcodeComputer ic) {
46+
List<Point> corridorSpaces = findPos(grid, PATH);
47+
for(Point p : corridorSpaces) {
48+
if(hasAdjecent(grid, p, UNEXPLORED)) {
49+
Grid2d map2d = new Grid2d(grid, false);
50+
Point[] route = map2d.findPath(START_POINT, findPos(grid, FINISH).get(0));
51+
traverseRoute(ic, pos, route);
52+
return p;
53+
}
54+
}
55+
return null;
56+
}
57+
58+
private void traverseRoute(IntcodeComputer ic, Point pos, Point[] route) {
59+
for(Point p : route) {
60+
ic.run(Direction.getByMove(pos, p).num);
61+
}
62+
}
63+
64+
private boolean hasAdjecent(int[][] grid, Point pos, int tile) {
65+
return grid[pos.y+1][pos.x] == tile || grid[pos.y][pos.x+1] == tile || grid[pos.y-1][pos.x] == tile || grid[pos.y][pos.x-1] == tile;
66+
}
67+
68+
private List<Point> findPos(int[][] grid, int tile) {
69+
List<Point> positions = new ArrayList<>();
70+
for(int y = 0; y<grid.length; y++) {
71+
for(int x = 0; x<grid[y].length; x++) {
72+
if(grid[y][x] == tile)
73+
positions.add(new Point(x, y));
74+
}
75+
}
76+
return positions;
77+
}
78+
79+
private void explore(int[][] grid, Point pos, IntcodeComputer ic) {
80+
Direction dir = Direction.NORTH;
81+
for(int i = 0; i<4;i++) {
82+
Point move = dir.move(pos);
83+
if(grid[move.y][move.x] == UNEXPLORED) {
84+
long run = ic.run(dir.num);
85+
System.out.println(run);
86+
grid[move.y][move.x] = Math.toIntExact(run);
87+
if(grid[move.y][move.x] != WALL) {
88+
ic.run(dir.opposite().num); // Move back
89+
}
90+
}
91+
dir = dir.turn(true);
3092
}
3193
}
3294

src/main/java/com/sbaars/adventofcode2019/pathfinding/Example.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.sbaars.adventofcode2019.pathfinding;
22

3+
import java.awt.Point;
4+
35
public class Example {
46
public Example() {
5-
double[][] map = { { 0, 1, 0, 0, 0 },
7+
int[][] map = { { 0, 1, 0, 0, 0 },
68
{ 0, 0, 0, 1, 0 },
79
{ 1, 1, 1, 1, 0} };
810
Grid2d map2d = new Grid2d(map, false);
9-
System.out.println(map2d.findPath(0, 0, 4, 2));
11+
System.out.println(map2d.findPath(new Point(0, 0), new Point(4, 2)));
1012
}
1113

1214
public static void main(String[] args) {

src/main/java/com/sbaars/adventofcode2019/pathfinding/Grid2d.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.sbaars.adventofcode2019.pathfinding;
22

3+
import java.awt.Point;
34
import java.util.HashSet;
4-
import java.util.List;
55
import java.util.Set;
66

77
/**
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818
public class Grid2d {
19-
private final double[][] map;
19+
private final int[][] map;
2020
private final boolean allowDiagonal;
2121

2222
/**
@@ -107,21 +107,24 @@ public boolean equals(Object obj) {
107107
return false;
108108
return true;
109109
}
110+
111+
public Point toPoint() {
112+
return new Point(x, y);
113+
}
110114

111115
private Grid2d getOuterType() {
112116
return Grid2d.this;
113117
}
114118

115119
}
116120

117-
public Grid2d(double[][] map, boolean allowDiagonal) {
121+
public Grid2d(int[][] map, boolean allowDiagonal) {
118122
this.map = map;
119123
this.allowDiagonal = allowDiagonal;
120124
}
121125

122-
public List<MapNode> findPath(int xStart, int yStart, int xGoal, int yGoal) {
123-
return PathFinding.doAStar(new MapNode(xStart, yStart), new MapNode(
124-
xGoal, yGoal));
126+
public Point[] findPath(Point start, Point end) {
127+
return PathFinding.doAStar(new MapNode(start.x, start.y), new MapNode(end.x, end.y)).stream().map(MapNode::toPoint).toArray(Point[]::new);
125128
}
126129

127130
}

src/main/resources/day17.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/resources/day18.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)