-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Not super happy with the until and while loops, but they're relatively clean so calling it good enough for today.
- Loading branch information
1 parent
2876ec9
commit d806fed
Showing
4 changed files
with
702 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
Oops, something went wrong.