Created
December 4, 2021 19:09
-
-
Save sarahzrf/079e5572e50d8cf05c2c9b94ea677dfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Board | |
def self.read(stream=$<) | |
new(5.times.map do | |
stream.gets.split.map(&:to_i) | |
end) | |
end | |
def initialize(grid) | |
@num_grid = grid | |
@mark_grid = grid.map {|row| row.map {false}} | |
@by_num = {} | |
grid.each_with_index do |row, i| | |
row.each_with_index do |n, j| | |
@by_num[n] = [i, j] | |
end | |
end | |
end | |
def play(n) | |
return unless (i, j = @by_num[n]) | |
@mark_grid[i][j] = true | |
return unless bingo? | |
score(n) | |
end | |
def bingo? | |
@mark_grid.any? {|row| row.all?} or | |
@mark_grid.transpose.any? {|col| col.all?} | |
end | |
def score(n) | |
unmarked_tot = 0 | |
@num_grid.each_with_index do |row, i| | |
row.each_with_index do |m, j| | |
unmarked_tot += m if !@mark_grid[i][j] | |
end | |
end | |
unmarked_tot * n | |
end | |
end | |
nums = gets.chomp.split(",").map(&:to_i) | |
boards = [] | |
loop do | |
gets or break | |
boards << Board.read | |
end | |
nums.each do |n| | |
boards.each do |board| | |
if score = board.play(n) | |
print score | |
exit | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment