24 lines
499 B
Ruby
Executable File
24 lines
499 B
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require_relative '../common'
|
|
|
|
class Day03 < Day
|
|
def self.priority(char)
|
|
(("a".."z").to_a + ("A".."Z").to_a).index(char) + 1
|
|
end
|
|
|
|
def part1
|
|
stdin.map(&:chars).map do |rucksack|
|
|
self.class.priority((rucksack[0..(rucksack.size / 2) - 1] & rucksack[(rucksack.size / 2)..]).first)
|
|
end.sum
|
|
end
|
|
|
|
def part2
|
|
stdin.map(&:chars).each_slice(3).map do |group|
|
|
self.class.priority group.inject { |res, g| res & g }.first
|
|
end.sum
|
|
end
|
|
end
|
|
|
|
Day03.run
|