Skip to content

Instantly share code, notes, and snippets.

@jarib
Created June 8, 2010 12:16
Show Gist options
  • Save jarib/429926 to your computer and use it in GitHub Desktop.
Save jarib/429926 to your computer and use it in GitHub Desktop.
def solve(input, name_index, left_index, right_index)
min, result = 100, nil
input.each_line do |line|
next unless line =~ /^\s*\d/
row = line.strip.split(/\s+/).map { |e| (digits = e[/\d+/]) ? digits.to_i : e }
name = row[name_index]
left = row[left_index]
right = row[right_index]
diff = (left - right).abs
min, result = diff, name if diff < min
end
result
end
p solve(File.read("part1/weather.dat"), 0, 1, 2) # => 14
p solve(File.read("part2/football.dat"), 1, 6, 8) # => "Aston_Villa"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment