In Kotlin I need to make sure all workstations are covered, so people can book time off, there are 4 workstations but people can only do 1 at a time. I need to ensure all Workstations have at least 1 person in to cover it, the issue I have with my code is that people are capable of multiple workstations
enum class WorkStations {
PANEL, OUTSIDE, MOVEMENTS, EXTRUDER
}
data class Operator(val name: String, val workStations: List<WorkStations> =
emptyList())
fun main() {
val array = arrayListOf(
Operator(
"Andy",
listOf(WorkStations.PANEL, WorkStations.OUTSIDE, WorkStations.MOVEMENTS)
),
Operator(
"Alan",
listOf(WorkStations.PANEL, WorkStations.OUTSIDE, WorkStations.MOVEMENTS)
),
Operator(
"Matt",
listOf(WorkStations.OUTSIDE)
),
Operator(
"Paul",
listOf(WorkStations.EXTRUDER, WorkStations.MOVEMENTS)
),
Operator(
"Jack",
listOf(WorkStations.EXTRUDER, WorkStations.MOVEMENTS)
),
Operator(
"James",
listOf(WorkStations.OUTSIDE)
),
Operator(
"Tall Paul",
),
Operator(
"Josh")
)
fun areWorkStationsCovered(array: ArrayList<Operator>): Boolean {
val newList = array.flatMap { it.workStations }.groupingBy { it }.eachCount().filter { it.value >= 1 }
println(newList)
return newList.size >= 4
}
println(areWorkStationsCovered(array))
}
returns:
{PANEL=2, OUTSIDE=4, MOVEMENTS=4, EXTRUDER=2}
true
But this isn't correct, as if Paul and Alan are off then this is returned:
{PANEL=1, OUTSIDE=3, MOVEMENTS=2, EXTRUDER=1}
true
It looks ok but its not correct as Jack would need to do the EXTRUDER therefore only 1 movements person and Andy would have to do the PANEL therefore no Movements person, I think I need to remove people from the original list but just cant think of a simplistic, functional approach any thoughts would be greatly appreciated...