Who will rule the realm?
You are given a family tree which contains: name, sex, date of birth, date of death and a list of heirs, for each family member. You may use any convenient (nested) list format to represent the tree. The following examples will use JSON. Determine who will rule the realm and in which order. The succession rules are the following:
- When a king dies the realm goes to its oldest legit male child.
- If there is none it passes to the oldest legit female child.
- In case the king had no heirs the realm goes to the oldest legit king's brother
- If there are no male brothers the realm will go to the oldest legit king's sister
- If all legit relatives are dead the realm goes to the closest bastard, applying the above rules
- If no legit nor bastard relatives are alive print
LINE EXTINCT
We define a legit son as one who has the same "house" name as its father.
Examples:
Input
{}
Output
LINE EXTINCT
Input
{
"name" : "Maegor",
"house" : "Targaryen",
"birth" : 12,
"death" : 48,
"sex" : "male",
"heirs" : []
}
Output 12-48: Maegor, LINE EXTINCT
Input
{
"name" : "Jaehaerys",
"house" : "Targaryen",
"birth" : 34,
"death" : 103,
"sex" : "male",
"heirs" : [
{
"name" : "Viserys",
"house" : "Targaryen",
"birth" : 77,
"death" : 129,
"sex" : "male",
"heirs" : []
}
]
}
Output 34-103: Jaehaerys, 103-129: Viserys, LINE EXTINCT
Input
{
"name" : "Eddard",
"house" : "Stark",
"birth" : 263,
"death" : 299,
"sex" : "male",
"heirs" : [
{
"name" : "Robb",
"house" : "Stark",
"birth" : 283,
"death" : 300,
"sex" : "male",
"heirs" : []
},
{
"name" : "Jon",
"house" : "Snow",
"birth" : 284,
"death" : 384,
"sex" : "male",
"heirs" : []
},
{
"name" : "Sansa",
"house" : "Stark",
"birth" : 286,
"death" : 320,
"sex" : "female",
"heirs" : []
},
{
"name" : "Arya",
"house" : "Stark",
"birth" : 289,
"death" : 350,
"sex" : "female",
"heirs" : []
},
{
"name" : "Brann",
"house" : "Stark",
"birth" : 290,
"death" : 315,
"sex" : "male",
"heirs" : []
},
{
"name" : "Rickon",
"house" : "Stark",
"birth" : 295,
"death" : 319,
"sex" : "male",
"heirs" : []
}
]
}
Output: 263-299: Eddard, 299-300: Robb, 300-315: Brann, 315-319: Rickon, 319-320: Sansa, 320-350: Arya, 350-384: Jon, LINE EXTINCT
Note that the output can be formatted in any way you wish, its only requirements is that it contains the time of reign and the name of the reigning king during those years. I've used kings from the ASOIAF as demonstrative example, however the code needs to be valid for all input.
You may assume that no twins will be born.
This is a code golf challenge, so the shortest code in bytes wins
Good luck!!!
In case the king had no sons the realm goes to the oldest legit king's brother
. It seems to conflict with point 2(If no legit son, then oldest legit daughter is crowned). Do you meanheirs
instead ofsons
in point 3? \$\endgroup\$