@@ -20,9 +20,9 @@ case class Person(
20
20
case _ if destination > position => Up
21
21
case _ if destination < position => Down
22
22
23
- def matchesDirection (lift : Lift ): Boolean = desiredDirection == lift.direction
24
- def isBelow (lift : Lift ): Boolean = position < lift.position
25
- def isAbove (lift : Lift ): Boolean = position > lift.position
23
+ inline def matchesDirection (lift : Lift ): Boolean = desiredDirection == lift.direction
24
+ inline def isBelow (lift : Lift ): Boolean = position < lift.position
25
+ inline def isAbove (lift : Lift ): Boolean = position > lift.position
26
26
}
27
27
28
28
case class Lift (
@@ -31,15 +31,15 @@ case class Lift(
31
31
people : Queue [Person ],
32
32
capacity : Int
33
33
) {
34
- def isFull : Boolean = people.size == capacity
35
- def hasRoom : Boolean = people.size != capacity
36
- def hasPeople : Boolean = people.nonEmpty
37
- def isEmpty : Boolean = people.isEmpty
34
+ val isFull : Boolean = people.size == capacity
35
+ val hasRoom : Boolean = people.size != capacity
36
+ val hasPeople : Boolean = people.nonEmpty
37
+ val isEmpty : Boolean = people.isEmpty
38
38
39
- def accepts (person : Person ): Boolean =
39
+ inline def accepts (person : Person ): Boolean =
40
40
hasRoom && person.desiredDirection == direction
41
41
42
- def nearestPassengerTarget : Option [Floor ] =
42
+ inline def nearestPassengerTarget : Option [Floor ] =
43
43
people.filter(_.matchesDirection(this )).map(_.destination).minByOption(floor => Math .abs(floor - position))
44
44
45
45
@ tailrec final def pickup (building : Building ): (Lift , Building ) =
@@ -52,14 +52,14 @@ case class Lift(
52
52
val emptierBuilding = building.copy(floors = building.floors.updated(position, emptierQueue))
53
53
fullerLift.pickup(emptierBuilding)
54
54
55
- def fixDirection (building : Building ): Lift = position match
55
+ inline def fixDirection (building : Building ): Lift = position match
56
56
case 0 => copy(direction = Up )
57
57
case p if p == building.floors.length - 1 => copy(direction = Down )
58
58
case _ => this
59
59
60
- def dropOff : Lift = copy(people = people.filter(_.destination != position))
60
+ inline def dropOff : Lift = copy(people = people.filter(_.destination != position))
61
61
62
- def align (building : Building ): Lift =
62
+ inline def align (building : Building ): Lift =
63
63
List (nearestPassengerTarget, building.nearestRequestInSameDirection(this )).flatten
64
64
.minByOption(floor => Math .abs(floor - position))
65
65
.match
@@ -79,19 +79,19 @@ case class Lift(
79
79
case class Building (
80
80
floors : Array [Queue [Person ]]
81
81
) {
82
- def isEmpty : Boolean = floors.forall(_.isEmpty)
83
- def hasPeople : Boolean = ! isEmpty
82
+ val isEmpty : Boolean = floors.forall(_.isEmpty)
83
+ val hasPeople : Boolean = ! isEmpty
84
84
85
- private def peopleGoing (direction : Direction ): List [Person ] =
85
+ inline private def peopleGoing (direction : Direction ): List [Person ] =
86
86
floors.flatMap(queue => queue.filter(_.desiredDirection == direction)).toList
87
87
88
- def lowestFloorGoingUp (lift : Lift ): Option [Floor ] =
88
+ inline def lowestFloorGoingUp (lift : Lift ): Option [Floor ] =
89
89
peopleGoing(Up ).filter(_.isBelow(lift)).map(_.position).minOption
90
90
91
- def highestFloorGoingDown (lift : Lift ): Option [Floor ] =
91
+ inline def highestFloorGoingDown (lift : Lift ): Option [Floor ] =
92
92
peopleGoing(Down ).filter(_.isAbove(lift)).map(_.position).maxOption
93
93
94
- def nearestRequestInSameDirection (lift : Lift ): Option [Floor ] =
94
+ inline def nearestRequestInSameDirection (lift : Lift ): Option [Floor ] =
95
95
lift.direction match
96
96
case Up => peopleGoing(Up ).filter(_.isAbove(lift)).map(_.position).minOption
97
97
case Down => peopleGoing(Down ).filter(_.isBelow(lift)).map(_.position).maxOption
@@ -102,15 +102,15 @@ case class LiftSystem(
102
102
lift : Lift ,
103
103
stops : List [Floor ]
104
104
) {
105
- private def fixDirection : LiftSystem = copy(lift = lift.fixDirection(building))
106
- private def dropOff : LiftSystem = copy(lift = lift.dropOff)
107
- private def align : LiftSystem = copy(lift = lift.align(building))
105
+ inline private def fixDirection : LiftSystem = copy(lift = lift.fixDirection(building))
106
+ inline private def dropOff : LiftSystem = copy(lift = lift.dropOff)
107
+ inline private def align : LiftSystem = copy(lift = lift.align(building))
108
108
109
- private def pickup : LiftSystem =
109
+ inline private def pickup : LiftSystem =
110
110
val (lift2, building2) = lift.pickup(building)
111
111
copy(lift = lift2, building = building2)
112
112
113
- private def registerStop : LiftSystem =
113
+ inline private def registerStop : LiftSystem =
114
114
stops.lastOption match
115
115
case Some (lastStop) if lastStop == lift.position => this
116
116
case _ => copy(stops = stops :+ lift.position)
0 commit comments