-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.rb
25 lines (25 loc) · 852 Bytes
/
3.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
#!ruby
values = []
$stdin.each_line do |line| values.append(line.strip) end
gamma = (0..11).map do |bit|
ones = values.filter do |v| v[bit] == "1" end.count
ones > values.count / 2 ? "1" : "0"
end.inject(&:+).to_i(2)
epsilon = (0..11).map do |bit|
ones = values.filter do |v| v[bit] == "1" end.count
ones <= values.count / 2 ? "1" : "0"
end.inject(&:+).to_i(2)
puts(gamma * epsilon)
def filter(values, less)
(0..11).each do |bit|
ones = values.filter do |v| v[bit] == "1" end
zeros = values.filter do |v| v[bit] == "0" end
if less
values = (zeros.count <= ones.count) ? zeros : ones
else
values = (zeros.count > ones.count) ? zeros : ones
end
if values.count == 1 then return values[0].to_i(2) end
end
end
puts(filter(values, false) * filter(values, true))