AoC2022/day03/solution.rb

26 lines
699 B
Ruby
Executable File

#!/bin/env ruby
require 'pry'
def item_priority(item)
adjust = (item == item.upcase ? 38 : 96)
item.ord - adjust
end
sacks = File.readlines('./input').map(&:strip).map { |sack| sack.chars.each_slice(sack.size / 2).map(&:join) }
items = sacks.map { |comp1, comp2| [comp1.chars.uniq, comp2.chars.uniq].flatten.sort.join.scan(/(.)\1/).flatten[0] }
items.map! { |item| item_priority(item) }
puts "Part 1 Solution: #{items.sum}"
## Part2
elves = sacks.map(&:join).each_slice(3).to_a
elves.map! do |group|
group.map { |item| item.chars.uniq.join }.join.chars.sort.join.scan(/(.)\1\1/).flatten[0]
end
elves.map! { |item| item_priority(item) }
puts "Part 2 Solution: #{elves.sum}"
#binding.pry