-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-04.rb
42 lines (35 loc) · 1.19 KB
/
day-04.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
# https://adventofcode.com/2021/day/4
# Giant Squid: checking if the array of drawn numbers intersects with Bingo board arrays
require_relative 'common'
class Day4 < AdventDay
def bingo?(board, draw)
(board + board.transpose).any? { |line| (draw & line).size == 5 }
end
def first_part
draw = input[:draw].first(4)
input[:draw].drop(4).each do |number|
draw << number
winner = input[:boards].find { |board| bingo?(board, draw) }
return number * (winner.flatten - draw).sum if winner
end
end
def second_part
boards = input[:boards] # just for convenience
draw = input[:draw].first(4)
input[:draw].drop(4).each do |number|
draw << number
winners, boards = boards.partition { |board| bingo?(board, draw) }
return number * (winners.last.flatten - draw).sum if boards.count.zero?
end
end
private
# Let's store the boards as 2D arrays; this way we can use #transpose to check rows and columns for bingo
def convert_data(data)
data = data.split("\n\n")
{
draw: data.first.split(',').map(&:to_i),
boards: data.drop(1).map { |block| block.split("\n").map { |row| row.split.map(&:to_i) } }
}
end
end
Day4.solve