-
Notifications
You must be signed in to change notification settings - Fork 68
/
DeadAnts.rb
35 lines (27 loc) · 898 Bytes
/
DeadAnts.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
=begin
An orderly trail of ants is marching across the park picnic area.
It looks something like this:
..ant..ant.ant...ant.ant..ant.ant....ant..ant.ant.ant...ant..
But suddenly there is a rumour that a dropped chicken sandwich has
been spotted on the ground ahead. The ants surge forward!
Oh No, it's an ant stampede!!
Some of the slower ants are trampled, and their poor little ant
bodies are broken up into scattered bits.
The resulting carnage looks like this:
...ant...ant..nat.ant.t..ant...ant..ant..ant.anant..t
Can you find how many ants have died?
=end
# My Solution
def deadAntCount(ants)
puts "ants = #{ants}"
return 0 if ants == nil
answer = 0
ants.gsub!("ant","")
new_array = [ants.count('a'),ants.count('n'),ants.count('t')]
new_array.max
end
# Better Solution
def deadAntCount(ants)
ants = ants.to_s.gsub('ant','')
%w(a n t).map { |c| ants.count(c) }.max
end