My solution can be found here:
http://linuz.sns.it/~capriotti/ruby/inference.rb

Paolo Capriotti