Skip to content

Commit 6a870bc

Browse files
committed
cleanup
GitPrivacy: NAruKXpTOOocG1SwKYydWS/sLr7ommjUR3Ii8Qraywnz8JjH3mqljWQPcH7TXCyGu2O4PfFN5Uo= ooDcPgHFNj/rMrppUZLJvG5aI0JsaLUD/zaUf25kJD8nSeCkRRpPAtL1g0bf8rXQzSG+x3V7FsA=
1 parent b829a3d commit 6a870bc

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/main/scala/ScalaPlayground/Lift/Immutable/LiftImmutable.scala

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import scala.collection.immutable.Queue
1010
type Floor = Int
1111
enum Direction { case Up, Down }
1212

13-
case class Person(position: Floor, destination: Floor) {
13+
case class Person(
14+
position: Floor,
15+
destination: Floor
16+
) {
1417
require(position != destination, "source and destination floor cannot be the same")
1518

1619
val desiredDirection: Direction = (position, destination) match
@@ -74,7 +77,9 @@ case class Lift(
7477
case None => copy(building.highestFloorGoingDown(this).getOrElse(0), Down)
7578
}
7679

77-
case class Building(floors: Array[Queue[Person]]) {
80+
case class Building(
81+
floors: Array[Queue[Person]]
82+
) {
7883
def isEmpty: Boolean = floors.forall(_.isEmpty)
7984
def hasPeople: Boolean = !isEmpty
8085

@@ -93,19 +98,28 @@ case class Building(floors: Array[Queue[Person]]) {
9398
case Down => peopleGoing(Down).filter(_.isBelow(lift)).map(_.position).maxOption
9499
}
95100

96-
case class LiftSystem(building: Building, lift: Lift, stops: List[Floor]) {
97-
def fixDirection: LiftSystem = copy(lift = lift.fixDirection(building))
98-
def dropOff: LiftSystem = copy(lift = lift.dropOff)
99-
def align: LiftSystem = copy(lift = lift.align(building))
101+
case class LiftSystem(
102+
building: Building,
103+
lift: Lift,
104+
stops: List[Floor]
105+
) {
106+
private def fixDirection: LiftSystem = copy(lift = lift.fixDirection(building))
107+
private def dropOff: LiftSystem = copy(lift = lift.dropOff)
108+
private def align: LiftSystem = copy(lift = lift.align(building))
100109

101-
def pickup: LiftSystem =
110+
private def pickup: LiftSystem =
102111
val (lift2, building2) = lift.pickup(building)
103112
copy(lift = lift2, building = building2)
104113

105-
def registerStop: LiftSystem =
114+
private def registerStop: LiftSystem =
106115
stops.lastOption match
107116
case Some(lastStop) if lastStop == lift.position => this
108117
case _ => copy(stops = stops :+ lift.position)
118+
119+
@tailrec final def resolve: LiftSystem =
120+
if building.isEmpty && lift.isEmpty && lift.position == 0
121+
then registerStop
122+
else registerStop.fixDirection.dropOff.pickup.align.resolve
109123
}
110124

111125
// Excuse the name. Dinglemouse.theLift() is how the function is called in the Codewars test suite
@@ -116,16 +130,10 @@ object Dinglemouse {
116130
queue.map(destination => Person(position = index, destination = destination)).to(Queue)
117131
}
118132

119-
val lift = Lift(position = 0, Direction.Up, people = Queue.empty, capacity)
120-
val building = Building(floors)
121-
122-
@tailrec def resolve(state: LiftSystem): LiftSystem =
123-
if state.building.isEmpty && state.lift.isEmpty && state.lift.position == 0
124-
then state
125-
else resolve(state.registerStop.fixDirection.dropOff.pickup.align)
126-
133+
val lift = Lift(position = 0, Direction.Up, people = Queue.empty, capacity)
134+
val building = Building(floors)
127135
val initialState = LiftSystem(building, lift, stops = List.empty)
128-
val finalState = resolve(initialState).registerStop
136+
val finalState = initialState.resolve
129137

130138
finalState.stops.toArray
131139
}

0 commit comments

Comments
 (0)