← Back to puzzles

004 The Tournament

In a world very similar to ours, people capture, "train" and do battle with special "monsters". Each monster is associated with an element, and has a Combat Power (CP) number that indicates its prowess in battle. A bunch of trainers want to arrange a tournament, where monsters face off against each other. The winner of a battle is the monster with the highest CP. However, if a monster is strong against its opponent's element, the monster's CP counts as double for that battle.

The elements, and their strengths, are:

🔥 Fire, strong against grass 🍃 Grass, strong against water 💧 Water, strong against fire

The tournament works like this: Given a list of monsters, pair each monster up with the monster next to it in the list, and decide who wins the first round of the tournament. The winners face each other in the second round of the tournament, and so on, until there is only one winner left. (For example, in a list of four contestants, the initial pairings will be number 1 and 2, and number 3 and 4).

The list of monsters contain the monster's name, their element and their CP. The program should output the name of the winner. If two monsters are tied, the monster appearing first in the list wins. You can assume that the input list is always a power of two.

For example, given the following list of monsters:

Vinotaur,Grass,40
Firazard,Fire,25
Splashitar,Water,50
Leekasaur,Grass,30

The output should be Firazard

Another example—given the following list of monsters:

Pyreon,Fire,100
Mastiking,Grass,90
Splashisaur,Water,95
Embreon,Fire,60
Petalord,Grass,200
Splashitar,Water,50
Flashion,Fire,50
Sproutler,Grass,50

The output should be Petalord

Please share your solution with your friends—you teach me, and I'll teach you!