Skip to content

Instantly share code, notes, and snippets.

@ninoseki
Created March 7, 2015 23:30
Show Gist options
  • Save ninoseki/6161d08436ba1faa12c8 to your computer and use it in GitHub Desktop.
Save ninoseki/6161d08436ba1faa12c8 to your computer and use it in GitHub Desktop.
Codeforces#294 Div2-A
# http://codeforces.com/contest/519/problem/A
class Board
WEIGHTS = { q: 9, r: 5, b: 3, n: 3, p: 1 }
def initialize(lines)
@pieces = lines.join.chars
end
def which_to_win?
case
when score('white') > score('black')
'White'
when score('white') == score('black')
'Draw'
else
'Black'
end
end
private
def score(color)
selector = (color == 'white') ? ('A'..'Z').to_a : ('a'..'z').to_a
@pieces.select { |piece| selector.include? piece }.map do |piece|
weight piece
end.inject(0, :+)
end
def weight(piece)
key = piece.downcase.to_sym
WEIGHTS.key?(key) ? WEIGHTS[key] : 0
end
end
lines = [].tap do |arr|
8.times { arr << gets.chomp }
end
board = Board.new(lines)
puts board.which_to_win?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment