Advent of Code solutions
1counts = []
2
3lines = ARGF.readlines()
4lines.each do |line|
5 line.chomp.chars.each_with_index do |char, i|
6 counts[i] ||= 0
7 counts[i] += 1 if char == "1"
8 end
9end
10
11def from_binary binary, digit
12 binary <<= 1
13 binary += 1 if digit
14 return binary
15end
16
17gamma = counts.map { |n| n >= lines.length / 2 }.reduce(0, &method(:from_binary))
18epsilon = counts.map { |n| n < lines.length / 2 }.reduce(0, &method(:from_binary))
19
20puts gamma * epsilon