Skip to content

Instantly share code, notes, and snippets.

@apneadiving
Created March 30, 2013 18:24
Show Gist options
  • Save apneadiving/5277785 to your computer and use it in GitHub Desktop.
Save apneadiving/5277785 to your computer and use it in GitHub Desktop.
syracuse
class Syracuse < Struct.new(:initial, :chiffre, :altitude, :duree_vol, :success)
HIGHER_THRESHOLD = 100000000000
def initialize(chiffre)
self.initial = self.chiffre = chiffre
self.altitude = self.duree_vol = 0
end
def next
with_backup do
self.chiffre = chiffre.even? ? chiffre / 2 : (chiffre * 3 ) + 1
end
end
def with_backup(&block)
self.altitude = chiffre if altitude < chiffre
self.duree_vol = duree_vol + 1
yield
end
def finished?
return false unless duree_vol > 0
return false unless [ HIGHER_THRESHOLD, 1 ].include? chiffre
self.success = chiffre == 1 ? true : false
end
end
class Runner < Struct.new(:borne_sup)
def go
results = get_results
max_duree_vol = results.max_by(&:duree_vol)
max_altitude = results.max_by(&:altitude)
puts "duree vol max: #{ max_duree_vol.duree_vol }, chiffre: #{max_duree_vol.initial}"
puts "altitude max: #{ max_altitude.altitude }, chiffre: #{max_altitude.initial}"
end
def get_results
(1..borne_sup).map do |chiffre|
syracuse = Syracuse.new(chiffre)
syracuse.next until syracuse.finished?
syracuse
end
end
end
Runner.new(1000).go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment