Skip to content

Commit

Permalink
Day 4: Bingo!
Browse files Browse the repository at this point in the history
Not super happy with the until and while loops, but they're relatively
clean so calling it good enough for today.
  • Loading branch information
adammathys committed Dec 4, 2021
1 parent 2876ec9 commit d806fed
Show file tree
Hide file tree
Showing 4 changed files with 702 additions and 0 deletions.
57 changes: 57 additions & 0 deletions 2021/04/bingo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Bingo
class Board
attr_reader :rows

def initialize(rows)
@rows = rows.map{_1.split.map(&:to_i)}
@columns = @rows.transpose
end

def won?(seen)
[*@rows, *@columns].any?{(_1 - seen).empty?}
end
end

def initialize(input)
@numbers = input[0].split(",").map(&:to_i)
@boards = input[1..-1].each_slice(6).map do |slice|
Board.new(slice[1..-1])
end
end

def winning_score
final_score(winning_board)
end

def losing_score
final_score(losing_board)
end

private

def final_score(board)
(board.rows.flatten - seen).sum * seen.last
end

def seen
@numbers[0..@last_called]
end

def winning_board
@last_called = 0
@last_called += 1 until (winning = @boards.find{_1.won?(seen)})
winning
end

def losing_board
@last_called = 0
boards = @boards.dup
while boards.length > 1
boards.reject!{_1.won?(seen)}
@last_called += 1
end
losing = boards.first
@last_called += 1 until losing.won?(seen)
losing
end
end
Loading

0 comments on commit d806fed

Please sign in to comment.