Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'rubygems'
require 'fattr'
require 'open-uri'
require 'singleton'
require 'enumerator'
class Team
attr_accessor :seed, :team
def initialize(team,seed)
@seed, @team = seed,team
end
def pom
Pom.instance.ratings[team]
end
def to_s
"##{seed} #{team} (#{pom})"
end
end
class Pom
include Singleton
fattr(:html) { open("http://kenpom.com") { |f| f.read } }
fattr(:team_lines) do
html.to_a.select { |x| x.strip =~ /^\d+ <a/ }
end
fattr(:ratings) do
team_lines.inject({}) do |h,ln|
team = ln.scan(/>(.*?)<\/a>/).flatten.first.downcase.gsub(/\./,"")
rating = ln.scan(/\d+-\d+ +(\.\d+) /).flatten.first.to_f
h.merge(team => rating)
end
end
end
class Game
attr_accessor :teams
def initialize(teams)
@teams = teams.sort_by { |x| x.seed }
end
fattr(:high) { teams.first }
fattr(:low) { teams[1] }
def upset?
low.team == winner.team
end
def factor
(high.seed == low.seed) ? 0.0 : 0.0085
end
def winner
(high.pom > low.pom + factor) ? high : low
end
def to_s
teams.join(" vs ") + ", Winner: #{winner.team} " + (upset? ? "(UPSET)" : "")
end
end
class Tourney
attr_accessor :teams
fattr(:teams) do
seeds = [1,16,8,9,5,12,4,13,6,11,3,14,7,10,2,15] * 4
$teams.split(",").zip(seeds).map do |x,seed|
Team.new(x.strip,seed)
end
end
fattr(:games) { [] }
def run!
while games.size < 63
winners = []
Enumerable::Enumerator.new(teams).each_slice(2) do |ts|
g = Game.new(ts)
winners << g.winner
self.games << g
end
@teams = winners
end
end
end
$teams = "louisville,morehead st,ohio st,siena,utah,arizona,wake forest,cleveland st,west virginia,dayton,kansas,north dakota st,boston college,southern california,michigan st,robert morris,connecticut,chattanooga,brigham young,texas a&m,purdue,northern iowa,washington,mississippi st,marquette,utah st,missouri,cornell,california,maryland,memphis,cal st northridge,pittsburgh,east tennessee st,oklahoma st,tennessee,florida st,wisconsin,xavier,portland st,ucla,virginia commonwealth,villanova,american,texas,minnesota,duke,binghamton,north carolina,radford,louisiana st,butler,illinois,western kentucky,gonzaga,akron,arizona st,temple,syracuse,stephen f austin,clemson,michigan,oklahoma,morgan st"
t = Tourney.new
t.run!
t.games.each { |x| puts x }
puts "Upsets: " + t.games.select { |x| x.upset? }.size.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.