-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_14.scala
187 lines (167 loc) · 5.74 KB
/
day_14.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
import scala.io.Source
object Day14 {
case class Point(x: Int, y: Int) {
def +(other: Point): Point = Point(x + other.x, y + other.y)
}
case class RockPath(points: List[Point]) {
// Technically this tests for the box formed by using the points
// as opposite corners, but that's fine since the "box" in this case
// is guaranteed to be a line with length in only one dimension.
def pointOccupied(point: Point): Boolean =
points.sliding(2).exists { case List(start, end) =>
val xs = List(start.x, end.x)
val ys = List(start.y, end.y)
Range.inclusive(xs.min, xs.max).contains(point.x) &&
Range.inclusive(ys.min, ys.max).contains(point.y)
}
def toPointSet: Set[Point] =
points
.sliding(2)
.map { case List(start, end) =>
val xs = List(start.x, end.x)
val ys = List(start.y, end.y)
Range
.inclusive(xs.min, xs.max)
.flatMap { x =>
Range.inclusive(ys.min, ys.max).map { y =>
Point(x, y)
}
}
.toSet
}
.reduce(_.union(_))
}
case class Simulation(
sandSource: Point,
rockPaths: List[RockPath],
simulateFloor: Boolean,
sandAtRest: Set[Point] = Set.empty,
complete: Boolean = false,
// An optimization to reduce the number of points simulated,
// since most sand units will follow the same path until they
// reach the previously placed point. For ease of use, it is
// modeled with the most recent point at the head.
previousSandPath: Option[List[Point]] = None
) {
val rockLocations = rockPaths.flatMap(_.toPointSet)
val points = rockPaths.map(_.toPointSet).reduce(_.union(_))
val minY = points.map(_.y).min
val maxY = points.map(_.y).max
val minX = points.map(_.x).min
val maxX = points.map(_.x).max
val floorHeight = maxY + 2
val MOVEMENT_OFFSETS: Seq[Point] =
Seq(Point(0, 1), Point(-1, 1), Point(1, 1))
sealed trait SimulationState(path: List[Point])
case class Active(at: Point, path: List[Point])
extends SimulationState(path)
case class Settled(at: Point, path: List[Point])
extends SimulationState(path)
case class Fallen(path: List[Point]) extends SimulationState(path)
def simulateSand: Option[(Point, List[Point])] = {
def stepSandSimulation(
activeUnit: Point,
pathSoFar: List[Point]
): SimulationState = {
if (!simulateFloor && activeUnit.y + 1 > maxY) {
return Fallen(pathSoFar)
}
val potentialNextPoints = MOVEMENT_OFFSETS.map(_ + activeUnit)
potentialNextPoints.find(!pointOccupied(_)) match {
case Some(destination) => Active(destination, activeUnit :: pathSoFar)
case None => Settled(activeUnit, pathSoFar)
}
}
val initialState = previousSandPath match {
case Some(h :: t) => Active(h, t)
case None => Active(sandSource, List.empty) // No sand simulated yet!
case Some(List()) =>
throw new RuntimeException("This should be unreachable")
}
val stateSequence =
LazyList.unfold[SimulationState, SimulationState](initialState) {
case Active(point, path) =>
val stepped = stepSandSimulation(point, path)
Some((stepped, stepped))
case other => None
}
stateSequence.last match {
case Settled(point, path) => Some(point, path)
case Fallen(_) => None
case Active(_, _) =>
throw new RuntimeException(
"Unexpected active sand unit at the end of a simulation"
)
}
}
def step: Simulation = {
if (pointOccupied(sandSource)) { copy(complete = true) }
else {
simulateSand match {
case Some((point, path)) =>
copy(
sandAtRest = sandAtRest + point,
previousSandPath = Some(path)
)
case None => copy(complete = true)
}
}
}
def pointOccupied(point: Point): Boolean =
sandAtRest.contains(point) ||
rockLocations.contains(point) ||
(simulateFloor && point.y == floorHeight)
override def toString: String = {
val lines = Range
.inclusive(0, maxY + 2)
.map { y =>
Range
.inclusive(minX, maxX)
.map { x =>
val point = Point(x, y)
if (sandAtRest.contains(point)) {
'o'
} else if (
rockLocations.contains(point) ||
(simulateFloor && point.y == maxY + 2)
) {
'#'
} else { '.' }
}
.mkString("")
}
lines.mkString("\n")
}
}
def main = {
val lines = Source.fromFile("day_14.input").getLines().toList
val rockPaths = lines.map(parseLine)
val sandSource = Point(500, 0)
val part1Simulation = Simulation(
sandSource = sandSource,
rockPaths = rockPaths,
simulateFloor = false
)
val part1TerminalState =
LazyList.iterate(part1Simulation)(_.step).takeWhile(!_.complete).last
println(s"Part 1: ${part1TerminalState.sandAtRest.size}")
val part2Simulation = Simulation(
sandSource = sandSource,
rockPaths = rockPaths,
simulateFloor = true
)
val part2TerminalState =
LazyList.iterate(part2Simulation)(_.step).takeWhile(!_.complete).last
println(s"Part 2: ${part2TerminalState.sandAtRest.size}")
}
def parseLine(line: String): RockPath =
RockPath(
line
.split(" -> ")
.map { pointStr =>
val Array(x, y) = pointStr.split(",")
Point(x.toInt, y.toInt)
}
.toList
)
}