-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.rb
49 lines (45 loc) · 1.17 KB
/
4.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!ruby
class Board
def initialize(numbers)
@numbers = numbers
end
def self.parse(input)
numbers = []
5.times do
numbers.append(input.gets.strip.split(" ").map(&:to_i))
end
return Board.new(numbers)
end
def mark(n)
@numbers.each_index do |row|
@numbers[row].each_index do |col|
if @numbers[row][col] == n then @numbers[row][col] = -1; end
end
end
end
def win?
5.times do |i|
return true if @numbers[i].all? do |n| n == -1 end
return true if @numbers.all? do |row| row[i] == -1 end
end
return false
end
def score(n)
return n * (@numbers.map do |row| row.sum do |i| [0,i].max end end.inject(&:+))
end
end
numbers = $stdin.gets.strip.split(",").map(&:to_i)
boards = []
while $stdin.gets != nil do boards.append(Board.parse($stdin)) end
score = -1
numbers.each do |n|
boards.each do |board|
next if board.win?
board.mark(n)
if board.win?
if score == -1 then puts(board.score(n)) end
score = board.score(n)
end
end
end
puts(score)