Skip to content

Instantly share code, notes, and snippets.

@plehoux
Last active December 12, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plehoux/4714875 to your computer and use it in GitHub Desktop.
Save plehoux/4714875 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
# Yeah Capybara is overkill... YEAH!
Capybara.run_server = false
Capybara.current_driver = :selenium
class Numeric
def add y
self + y
end
def sub y
self - y
end
def mult y
self * y
end
def div y
self / y
end
end
class Solver
include Capybara::DSL
def initialize param
@param = param
begin
solve "/#{@param}"
rescue SyntaxError => se
find_longest_sequence
end
end
def solve url
visit url
answer = eval(find('body').text)
store answer
solve "/#{answer}#{@param}"
end
def store answer
@seq ||= [[]]
active_seq = @seq.last
if active_seq.empty? or active_seq.last < answer
active_seq << answer
else
@seq << [answer]
end
end
def find_longest_sequence
longest = []
for seq in @seq
if longest.empty? or (seq.length >= longest.length and seq.reduce(:+) > longest.reduce(:+))
longest = seq
end
end
puts "Sum of longest sequence : #{longest.reduce(:+)}"
end
end
Capybara.app_host = 'http://challenge.airtime.com'
Solver.new '?email=plehoux@gmail.com'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment