-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.java
185 lines (159 loc) · 5.95 KB
/
Day12.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.util.ArrayList;
import java.util.List;
public class Day12 {
public static void Run(List<String> input) {
var map = new Map(input);
var shortestPath = FindShortestPath(map, map.getStart());
System.out.println("Day 12 p1, shortest path is "+(shortestPath.size()-1)+" steps");
p2(map);
}
public static void p2(Map map)
{
int shortestLength = Integer.MAX_VALUE;
for (int y=0; y < map.getHeight(); y++){
for (int x = 0; x < map.getWidth(); x++){
var point = map.getPoint(x,y);
if (point.getElevation()=='a')
{
resetNavigation(map);
var path = FindShortestPath(map, point);
if ((path != null) && (path.size() < shortestLength))
shortestLength = path.size();
}
}
}
System.out.println("Day 12 p1, shortest path from any 'a' is "+(shortestLength-1)+" steps");
}
public static List<MapPoint> FindShortestPath(Map map, MapPoint start)
{
// based on https://en.wikipedia.org/w/index.php?title=Dijkstra%27s_algorithm&oldid=1127202995#Using_a_priority_queue
var destination = map.getDestination();
start.setBestPredecssor(null, 0);
var Q = new ArrayList<MapPoint>();
Q.add(start);
boolean foundPath=false;
while (!Q.isEmpty() && !foundPath)
{
// get next point to evaluate
var u = Q.remove(0);
var neighbors = getLegalNextSteps(map, u);
for (var v : neighbors)
{
// not bothering to check alternate distance like
// the wikipedia article because all edge lengths are 1 so
// previous best plus new edge length can't be less than
// the previous best
if ((v!= start) && (v.getBestPredecssor() == null))
{
Q.add(v);
v.setBestPredecssor(u, u.getBestDistance()+1);
if (v == destination){
foundPath=true;
break;
}
}
}
}
if (!foundPath)
return null;
// build path starting with destination;
var path = new ArrayList<MapPoint>();
var point = destination;
do{
path.add(0,point);
point = point.getBestPredecssor();
}while (point != null);
return path;
}
public static List<MapPoint> getLegalNextSteps(Map map, MapPoint point) {
var nextSteps = new ArrayList<MapPoint>();
MapPoint next = map.getPoint(point.getX(), point.getY() - 1);
if (IsLegalNextStep(point, next))
nextSteps.add(next);
next = map.getPoint(point.getX(), point.getY() + 1);
if (IsLegalNextStep(point, next))
nextSteps.add(next);
next = map.getPoint(point.getX() - 1, point.getY());
if (IsLegalNextStep(point, next))
nextSteps.add(next);
next = map.getPoint(point.getX() + 1, point.getY());
if (IsLegalNextStep(point, next))
nextSteps.add(next);
return nextSteps;
}
public static void resetNavigation(Map map)
{
for (int y=0; y < map.getHeight(); y++){
for (int x=0; x < map.getWidth(); x++)
map.getPoint(x, y).setBestPredecssor(null, Integer.MAX_VALUE);
}
}
public static boolean IsLegalNextStep(MapPoint current, MapPoint next)
{
if (next==null)
return false;
var elevationChange = next.getElevation() - current.getElevation();
return (elevationChange <= 1);
}
private static class Map {
int _width;
int _height;
MapPoint[][] _points;
MapPoint _start;
MapPoint _destination;
public Map(List<String> mapData) {
_height = mapData.size();
_width = mapData.get(0).length();
_points = new MapPoint[_height][_width];
for (int y = 0; y < _height; y++) {
var line = mapData.get(y);
for(int x=0; x < _width; x++){
var elevation = line.charAt(x);
_points[y][x]=new MapPoint(x,y,elevation);
if (elevation == 'S'){
_start=_points[y][x];
_start.setElevation('a');
}
else if (elevation == 'E'){
_destination=_points[y][x];
_destination.setElevation('z');
}
}
}
}
public MapPoint getStart(){return _start;}
public MapPoint getDestination(){return _destination;}
public int getWidth(){return _width;}
public int getHeight(){return _height;}
public MapPoint getPoint(int x, int y)
{
if ((x < 0) || (x >= _width) || (y < 0) ||(y >= _height))
return null;
return _points[y][x];
}
}
private static class MapPoint {
int _x;
int _y;
char _elevation;
MapPoint _bestPredecessor;
int _bestDistance=Integer.MAX_VALUE;
public MapPoint(int x, int y,char elevation) {
_x = x;
_y = y;
_elevation = elevation;
}
public void setElevation(char elevation){
_elevation = elevation;
}
public int getX(){return _x;}
public int getY(){return _y;}
public char getElevation(){return _elevation;}
public MapPoint getBestPredecssor(){return _bestPredecessor;}
public int getBestDistance(){return _bestDistance;}
public void setBestPredecssor(MapPoint newBest, int newBestDistance){
_bestPredecessor = newBest;
_bestDistance = newBestDistance;
}
}
}