45 lines
1.1 KiB
Ruby
45 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
module Year2023
|
|
class Day01 < Solution
|
|
# @input is available if you need the raw data input
|
|
# Call `data` to access either an array of the parsed data, or a single record for a 1-line input file
|
|
|
|
NUMBERS = {
|
|
'one' => 1,
|
|
'two' => 2,
|
|
'three' => 3,
|
|
'four' => 4,
|
|
'five' => 5,
|
|
'six' => 6,
|
|
'seven' => 7,
|
|
'eight' => 8,
|
|
'nine' => 9
|
|
}
|
|
|
|
def part_1
|
|
data.map do |line|
|
|
digits = line.gsub(/[^0-9]/, '')
|
|
"#{digits[0]}#{digits[-1]}".to_i
|
|
end.sum
|
|
end
|
|
|
|
def part_2
|
|
data.map do |line|
|
|
matches = line.scan(/(?=([0-9]|#{NUMBERS.keys.join('|')}))/).flatten
|
|
"#{NUMBERS[matches.first] || matches.first}#{NUMBERS[matches.last] || matches.last}".to_i
|
|
end.sum
|
|
end
|
|
|
|
private
|
|
# Processes each line of the input file and stores the result in the dataset
|
|
# def process_input(line)
|
|
# line.map(&:to_i)
|
|
# end
|
|
|
|
# Processes the dataset as a whole
|
|
# def process_dataset(set)
|
|
# set
|
|
# end
|
|
end
|
|
end
|