-
Notifications
You must be signed in to change notification settings - Fork 6
/
ts.ts
59 lines (46 loc) · 1.52 KB
/
ts.ts
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
import readlineSync from 'readline-sync'
const readline = () => readlineSync.prompt({ encoding: 'utf-8', prompt: '' })
// ------ Everything above this line will get cut when running copy script
class Vertex {
public links: Vertex[] = []
constructor(
public id: number,
public name: string,
public parent: string,
public birth: number,
public death: string,
public religion: string,
public gender: string
) {}
public addLink(toLink: Vertex) {
if (this.name === toLink.parent) this.links.push(toLink)
else this.links.forEach(aLink => aLink.addLink(toLink))
}
public toString(): string {
return this.links.length > 0 ? `${this.name} [${this.links.map(x => x.toString()).join(', ')}]` : `${this.name}`
}
public getTreeOutput(): Vertex[] {
const output: Vertex[] = []
if (this.death === '-' && this.religion !== 'Catholic') output.push(this)
this.links
.sort((a, b) => {
if (a.gender === b.gender) return a.birth - b.birth
return a.gender === 'M' ? -1 : 1
})
.forEach(aLink => output.push(...aLink.getTreeOutput()))
return output
}
}
let root: Vertex = null as any
const n: number = parseInt(readline())
for (let i = 0; i < n; i++) {
const [name, parent, birth, death, religion, gender] = readline().split(' ')
const v = new Vertex(i, name, parent, +birth, death, religion, gender)
if (!root) root = v
else root.addLink(v)
}
// console.error(root.toString())
root
.getTreeOutput()
.map(x => x.name)
.forEach(x => console.log(x))