@@ -71,7 +71,7 @@ case class Lift(
71
71
case Down =>
72
72
building.lowestFloorGoingUp(this ) match
73
73
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 )
75
75
}
76
76
77
77
case class Building (floors : Array [Queue [Person ]]) {
@@ -94,29 +94,18 @@ case class Building(floors: Array[Queue[Person]]) {
94
94
}
95
95
96
96
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))
99
100
100
- private def dropOff : LiftSystem =
101
- copy(lift = lift.dropOff)
102
-
103
- private def pickup : LiftSystem =
101
+ def pickup : LiftSystem =
104
102
val (lift2, building2) = lift.pickup(building)
105
103
copy(lift = lift2, building = building2)
106
104
107
- private def align : LiftSystem =
108
- copy(lift = lift.align(building))
109
-
110
105
def registerStop : LiftSystem =
111
106
stops.lastOption match
112
107
case Some (lastStop) if lastStop == lift.position => this
113
108
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
120
109
}
121
110
122
111
// Excuse the name. Dinglemouse.theLift() is how the function is called in the Codewars test suite
@@ -131,11 +120,13 @@ object Dinglemouse {
131
120
val building = Building (floors)
132
121
133
122
@ 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)
135
126
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
138
129
139
- finalState.registerStop. stops.toArray
130
+ finalState.stops.toArray
140
131
}
141
132
}
0 commit comments