Skip to content

Commit b829a3d

Browse files
committed
cleanup
GitPrivacy: 6fNekTnxgr8PQeD3Da1a8QYoCXVO3Ob9QwIS/whTVVU2me4JpdYVd7xfBi/CbxU2dcdUedirWbM= hTkjYKpPLjtv48APfLfff8j/4nmCEX1V2tQ1FVdymGMyA8C17gMaBBJaKiAHzvalmrbcR4ZCh0I=
1 parent ac9e63d commit b829a3d

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ case class Lift(
7171
case Down =>
7272
building.lowestFloorGoingUp(this) match
7373
case Some(lowest) => copy(lowest, Up)
74-
case None => copy(building.highestFloorGoingDown(this).getOrElse(0), Down)
74+
case None => copy(building.highestFloorGoingDown(this).getOrElse(0), Down)
7575
}
7676

7777
case class Building(floors: Array[Queue[Person]]) {
@@ -94,29 +94,18 @@ case class Building(floors: Array[Queue[Person]]) {
9494
}
9595

9696
case class LiftSystem(building: Building, lift: Lift, stops: List[Floor]) {
97-
private def fixDirection: LiftSystem =
98-
copy(lift = lift.fixDirection(building))
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))
99100

100-
private def dropOff: LiftSystem =
101-
copy(lift = lift.dropOff)
102-
103-
private def pickup: LiftSystem =
101+
def pickup: LiftSystem =
104102
val (lift2, building2) = lift.pickup(building)
105103
copy(lift = lift2, building = building2)
106104

107-
private def align: LiftSystem =
108-
copy(lift = lift.align(building))
109-
110105
def registerStop: LiftSystem =
111106
stops.lastOption match
112107
case Some(lastStop) if lastStop == lift.position => this
113108
case _ => copy(stops = stops :+ lift.position)
114-
115-
def isDone: Boolean =
116-
building.isEmpty && lift.isEmpty && lift.position == 0
117-
118-
def step: LiftSystem =
119-
registerStop.fixDirection.dropOff.pickup.align
120109
}
121110

122111
// Excuse the name. Dinglemouse.theLift() is how the function is called in the Codewars test suite
@@ -131,11 +120,13 @@ object Dinglemouse {
131120
val building = Building(floors)
132121

133122
@tailrec def resolve(state: LiftSystem): LiftSystem =
134-
if state.isDone then state else resolve(state.step)
123+
if state.building.isEmpty && state.lift.isEmpty && state.lift.position == 0
124+
then state
125+
else resolve(state.registerStop.fixDirection.dropOff.pickup.align)
135126

136-
val initialState = LiftSystem(building = building, lift = lift, stops = List.empty)
137-
val finalState = resolve(initialState)
127+
val initialState = LiftSystem(building, lift, stops = List.empty)
128+
val finalState = resolve(initialState).registerStop
138129

139-
finalState.registerStop.stops.toArray
130+
finalState.stops.toArray
140131
}
141132
}

0 commit comments

Comments
 (0)