-
Notifications
You must be signed in to change notification settings - Fork 68
/
GoodvEvil.rb
67 lines (52 loc) · 2.14 KB
/
GoodvEvil.rb
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
=begin
Middle Earth is about to go to war. The forces of good will have many battles
with the forces of evil. Different races will certainly be involved.
Each race has a certain 'worth' when battling against others. On the side of
good we have the following races, with their associated worth:
Hobbits - 1
Men - 2
Elves - 3
Dwarves - 3
Eagles - 4
Wizards - 10
On the side of evil we have:
Orcs - 1
Men - 2
Wargs - 2
Goblins - 2
Uruk Hai - 3
Trolls - 5
Wizards - 10
Although weather, location, supplies and valor play a part in any battle,
if you add up the worth of the side of good and compare it with the worth of
the side of evil, the side with the larger worth will tend to win.
Thus, given the count of each of the races on the side of good, followed by
the count of each of the races on the side of evil, determine which side wins.
Input:
The function will be given two parameters. Each parameter will be a string
separated by a single space. Each string will contain the count of each race
on the side of good and evil.
The first parameter will contain the count of each race on the side of good
in the following order:
Hobbits, Men, Elves, Dwarves, Eagles, Wizards.
The second parameter will contain the count of each race on the side of evil
in the following order:
Orcs, Men, Wargs, Goblins, Uruk Hai, Trolls, Wizards.
All values are non-negative integers. The resulting sum of the worth for each
side will not exceed the limit of a 32-bit integer.
Output:
Return ""Battle Result: Good triumphs over Evil" if good wins,
"Battle Result: Evil eradicates all trace of Good" if evil wins,
or "Battle Result: No victor on this battle field" if it ends in a tie.
=end
# My Solution
def goodVsEvil(good, evil)
good_side = [1,2,3,3,4,10]
evil_side = [1,2,2,2,3,5,10]
good_total = 0
evil_total = 0
good.split(" ").each_with_index {|x,i| good_total += good_side[i] * x.to_i}
evil.split(" ").each_with_index {|x,i| evil_total += evil_side[i] * x.to_i}
return "Battle Result: No victor on this battle field" if good_total == evil_total
good_total > evil_total ? "Battle Result: Good triumphs over Evil" : "Battle Result: Evil eradicates all trace of Good"
end