-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4_2.rb
73 lines (61 loc) · 1.36 KB
/
day4_2.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
file_data = File.read("day4.txt").split
number_stream=file_data.shift.split(',')
puts(file_data[0])
arrays=(0...file_data.length).inject([]) do |arrays, index|
if index%25==0
arrays<<[[file_data[index].to_i]]
elsif index%5==0
arrays[index/25]<<[file_data[index].to_i]
else
arrays[index/25][(index/5)%5]<<file_data[index].to_i
end
arrays
end
def play_bingo(number_stream, arrays)
number_stream.each do |number|
bingos=0
arrays=arrays.map do |grid|
grid=grid.map do |row|
row=row.map do |x|
if x==number.to_i
-1
else
x
end
end
row
end
grid
end
arrays.each do |grid|
grid.each do |row|
if row.sum==-5
if arrays.length>1
arrays.delete(grid)
else
return [grid, number.to_i]
end
end
end
end
arrays.each do |grid|
grid.transpose.each do |column|
if column.sum==-5
if arrays.length>1
arrays.delete(grid)
else
return [grid, number.to_i]
end
end
end
end
puts(bingos)
end
end
results=play_bingo(number_stream, arrays)
grid_values=results[0].flatten.map{|x| x<0 ? 0:x}
puts(grid_values.sum)
puts("The grid values are...")
puts(grid_values)
puts(results[1])
puts(grid_values.sum*results[1])