def run(f) len = 0 bits = [] f.each do |line| if len == 0 # Skip the trailing newline character and convert to 0-based index len = line.length() - 2 0.upto(len) { |n| bits[n] = [0, 0] } end len.downto(0) { |n| digit = Integer(line[n]) bits[n][digit] += 1 } end gamma = 0 epsilon = 0 len.downto(0) { |n| # Since we iterated from left to right on the input # we need to do the same when building the integers width = len - n if bits[n][0] > bits[n][1] gamma += 1 << width else epsilon += 1 << width end } return gamma * epsilon end