AoC2021/day03/solution.rb

21 lines
413 B
Ruby
Raw Normal View History

2021-12-03 15:29:07 +00:00
#!/bin/env ruby
#part 1
report = File.readlines('./input').map(&:strip)
masks = []
report[0].length.times{ masks.push(2 ** _1) }
report.map!{ _1.to_i(2) }
gamma = 0
masks.each do |mask|
if report.map{ mask & _1 }.reject(&:zero?).count > report.count / 2
gamma += mask
end
end
epsilon = gamma ^ masks.sum
puts "Gamma is #{gamma}"
puts "Epsilon is #{epsilon}"
puts "Power is #{gamma * epsilon}"