-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.scala
225 lines (188 loc) · 6.36 KB
/
main.scala
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import scala.io.Source
import scala.annotation.tailrec
import scala.collection.mutable
object Aoc {
case class DirectedEdge(from: Int, to: Int, weight: Double = 1.0) {
override def toString(): String = s"$from -> $to"
}
case class EdgeWeightedDigraph(
adj: Map[Int, List[DirectedEdge]] = Map.empty
) {
def addEdge(edge: DirectedEdge): EdgeWeightedDigraph = {
val list = this.adj.getOrElse(edge.from, List.empty)
val adj = this.adj + (edge.from -> (list :+ edge))
EdgeWeightedDigraph(adj)
}
}
object ShortestPath {
def run(
graph: EdgeWeightedDigraph,
start: Int
): Either[String, ShortestPathCalc] = {
val size = graph.adj.size
if (start >= size) Left(s"Source vertex must in range [0, $size)")
else {
val edgeTo = mutable.ArrayBuffer.fill[Option[DirectedEdge]](size)(None)
val distTo = mutable.ArrayBuffer.fill(size)(Double.PositiveInfinity)
distTo(start) = 0.0
val sourceDist = (start, distTo(start))
val sortByWeight: Ordering[(Int, Double)] = (a, b) =>
a._2.compareTo(b._2)
val queue =
mutable.PriorityQueue[(Int, Double)](sourceDist)(sortByWeight)
while (queue.nonEmpty) {
val (minDestV, _) = queue.dequeue()
val edges = graph.adj.getOrElse(minDestV, List.empty)
edges.foreach { e =>
if (distTo(e.to) > distTo(e.from) + e.weight) {
distTo(e.to) = distTo(e.from) + e.weight
edgeTo(e.to) = Some(e)
if (!queue.exists(_._1 == e.to))
queue.enqueue((e.to, distTo(e.to)))
}
}
}
Right(new ShortestPathCalc(edgeTo.toSeq, distTo.toSeq))
}
}
}
class ShortestPathCalc(
edgeTo: Seq[Option[DirectedEdge]],
distTo: Seq[Double]
) {
def pathTo(v: Int): Either[String, Seq[DirectedEdge]] = {
@tailrec
def go(list: List[DirectedEdge], vv: Int): List[DirectedEdge] =
edgeTo(vv) match {
case Some(e) => go(e +: list, e.from)
case None => list
}
hasPath(v).map(b => if (!b) Seq() else go(List(), v))
}
def hasPath(v: Int): Either[String, Boolean] =
distTo
.lift(v)
.map(_ < Double.PositiveInfinity)
.toRight(s"Vertex $v does not exist")
def distToV(v: Int): Either[String, Double] =
distTo.lift(v).toRight(s"Vertex $v does not exist")
}
val aElevation = 'a'.toInt
def charToElevation(c: Char): Int = c match {
case 'S' => charToElevation('a')
case 'E' => charToElevation('z')
case _ => c - aElevation
};
type Grid = Seq[Seq[Char]]
def mkGridIndex(row: Int, col: Int, ncol: Int): Int = (row * ncol) + col
def getEdges(
grid: Grid,
row: Int,
col: Int
): Array[DirectedEdge] = {
val nrow = grid.length
val ncol = grid(0).length
val from = mkGridIndex(row = row, col = col, ncol = ncol)
val elevation = charToElevation(grid(row)(col))
var edges: Array[DirectedEdge] = Array.empty
// The destination square can be at most one higher than the elevation of your current square.
// This also means that the elevation of the destination square can be much lower
// than the elevation of your current square.
val upRow = row - 1
if (upRow >= 0) {
val up = charToElevation(grid(upRow)(col))
val diff = up - elevation
if (diff <= 1)
edges :+= DirectedEdge(
from = from,
to = mkGridIndex(row = upRow, col = col, ncol = ncol)
)
}
val downRow = row + 1
if (downRow < nrow) {
val down = charToElevation(grid(downRow)(col))
val diff = down - elevation
if (diff <= 1)
edges :+= DirectedEdge(
from = from,
to = mkGridIndex(row = downRow, col = col, ncol = ncol)
)
}
val leftCol = col - 1
if (leftCol >= 0) {
val left = charToElevation(grid(row)(leftCol))
val diff = left - elevation
if (diff <= 1)
edges :+= DirectedEdge(
from = from,
to = mkGridIndex(row = row, col = leftCol, ncol = ncol)
)
}
val rightCol = col + 1
if (rightCol < ncol) {
val right = charToElevation(grid(row)(rightCol))
val diff = right - elevation
if (diff <= 1)
edges :+= DirectedEdge(
from = from,
to = mkGridIndex(row = row, col = rightCol, ncol = ncol)
)
}
edges
}
def buildInputGraph(grid: Grid): EdgeWeightedDigraph = {
val graph = EdgeWeightedDigraph(
grid.flatten.indices
.foldLeft[Map[Int, List[DirectedEdge]]](Map.empty)({ (m, i) =>
m + (i -> List.empty)
})
)
grid.zipWithIndex.foldLeft(graph)({ (g, x) =>
val (row, rowIndex) = x
row.zipWithIndex
.foldLeft(g)({ (gg, y) =>
val (char, colIndex) = y
if (char == 'E') gg
else
getEdges(grid, row = rowIndex, col = colIndex).foldLeft(gg)(
_.addEdge(_)
)
})
})
}
def partOne(inputFile: String): Int = {
val inputLines = Source.fromFile(inputFile).getLines()
val grid: Grid = inputLines.map(_.toSeq).toSeq
val start = grid.flatten.indexWhere(_ == 'S')
val end = grid.flatten.indexWhere(_ == 'E')
val graph = buildInputGraph(grid);
val shortest = ShortestPath.run(graph, start).toOption.get
val path = shortest.pathTo(end).toOption.get
path.length
}
def partTwo(inputFile: String): Int = {
val inputLines = Source.fromFile(inputFile).getLines()
val grid: Grid = inputLines.map(_.toSeq).toSeq
val starts = grid.flatten.zipWithIndex.flatMap({ case (char, i) =>
if (charToElevation(char) == 0) Some(i) else None
})
val end = grid.flatten.indexWhere(_ == 'E')
val graph = buildInputGraph(grid);
starts
.flatMap(start => {
val shortest = ShortestPath.run(graph, start).toOption.get
val path = shortest.pathTo(end).toOption.get
if (path.isEmpty) None else Some(path.length)
})
.min
}
def main(args: Array[String]) = {
val inputFile = args(0)
val partOneAnswer = partOne(inputFile)
assert(partOneAnswer == 339, "wrong answer to part one")
println(s"part one: $partOneAnswer")
val partTwoAnswer = partTwo(inputFile)
assert(partTwoAnswer == 332, "wrong answer to part two")
println(s"part two: $partTwoAnswer")
}
}