Created
December 4, 2021 19:18
-
-
Save sarahzrf/271ca96ea899b0054ebff3a5f07cc07f 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 | |
last_score = 0 | |
nums.each do |n| | |
boards = boards.filter do |board| | |
score = board.play(n) | |
last_score = score if score | |
!score | |
end | |
end | |
print(last_score) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment