-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathts.ts
64 lines (54 loc) · 2.02 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
60
61
62
63
64
import readlineSync from 'readline-sync'
const readline = () => readlineSync.prompt({ encoding: 'utf-8', prompt: '' })
// ------ Everything above this line will get cut when running copy script
enum Sign {
Rock = 'R',
Paper = 'P',
Scissors = 'C',
Lizard = 'L',
Spock = 'S'
}
type Player = { id: number; sign: Sign; winHistory: number[]; lost: boolean }
const whoWin = (a: Player, b: Player) => {
if (a.sign === Sign.Lizard && b.sign === Sign.Paper) return a
if (a.sign === Sign.Lizard && b.sign === Sign.Spock) return a
if (a.sign === Sign.Paper && b.sign === Sign.Rock) return a
if (a.sign === Sign.Paper && b.sign === Sign.Spock) return a
if (a.sign === Sign.Rock && b.sign === Sign.Lizard) return a
if (a.sign === Sign.Rock && b.sign === Sign.Scissors) return a
if (a.sign === Sign.Scissors && b.sign === Sign.Lizard) return a
if (a.sign === Sign.Scissors && b.sign === Sign.Paper) return a
if (a.sign === Sign.Spock && b.sign === Sign.Rock) return a
if (a.sign === Sign.Spock && b.sign === Sign.Scissors) return a
if (a.sign === b.sign && a.id < b.id) return a
return b
}
let players: Player[] = []
const n = parseInt(readline())
for (let i = 0; i < n; i++) {
const inputs = readline().split(' ')
players.push({ id: parseInt(inputs[0]), sign: inputs[1] as Sign, winHistory: [], lost: false })
}
console.error('Init players')
console.error(players)
while (players.length > 1) {
for (let i = 0, m = players.length; i < m; i += 2) {
const p1 = players[i]
const p2 = players[i + 1]
console.error('p1 ' + JSON.stringify(p1))
console.error('p2 ' + JSON.stringify(p2))
const p1Won = whoWin(p1, p2).id === p1.id
console.error(`p1=(id=${p1.id}, ${p1.sign}) vs p2=(id=${p2.id}, ${p2.sign}) -> ${p1 ? 'p1' : 'p2'}`)
if (p1Won) {
p1.winHistory.push(p2.id)
p2.lost = true
} else {
p2.winHistory.push(p1.id)
p1.lost = true
}
}
players = players.filter(x => !x.lost)
console.error(players)
}
console.log(players[0].id)
console.log(players[0].winHistory.join(' '))