-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathorDer-oF-succeSsion.kt
70 lines (60 loc) · 2.09 KB
/
orDer-oF-succeSsion.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package easy.orderOfSuccession
import java.util.*
// https://www.codingame.com/ide/puzzle/order-of-succession
enum class Gender(val gender: String) {
M("M"), F("F")
}
data class Person(
val name: String,
val parent: String,
val birth: Long,
val death: Long,
val isAnglican: Boolean,
val gender: Gender
)
fun main(args: Array<String>) {
val input = Scanner(System.`in`)
val n = input.nextInt()
val persons = LinkedList<Person>()
// Each person insert to List
for (i in 0 until n) {
val person = Person(
input.next(),
input.next(),
input.nextLong(),
run {
val temp = input.next()
if (temp == "-") 0 else temp.toLong()
},
run { input.next() == "Anglican" },
Gender.valueOf(input.next())
)
persons.addLast(person)
}
// Ordering rules (a) in order of generation (b) in order of gender (c) in order of age (year of birth)
// First sort - persons sorted by parents
val succession = persons.sortedWith(compareBy({ it.parent }, { it.gender }, { it.birth })).toMutableList()
// Sub sort - persons sorted by successors
for (i in succession.indices) {
val parent = succession[i].name
for (k in i + 1..succession.size) {
val targetIndex = succession.indexOfFirst {
it.parent == parent && k <= succession.indexOf(it)
}
succession.pushBefore(k, targetIndex)
}
}
// (a) exclude dead people (b) exclude people who are catholic (but include siblings of catholic people)
succession.filter { it.death == 0.toLong() && it.isAnglican }.forEach { println(it.name) }
}
private fun <E> MutableList<E>.pushBefore(pushBefore: Int, targetIndex: Int) {
for (i in pushBefore..targetIndex) {
this.swap(i, targetIndex)
}
}
fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
if (index1 == -1 || index2 == -1) return
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}