Snippet

advent_of_code/2023_1.rb
Code:
# Advent of Code 2023, Day 1: Trebuchet?!

# https://adventofcode.com/2023/day/1

require_relative './helpers.rb'

DIGITS = {
    '1' => '1', 'one'   => '1',
    '2' => '2', 'two'   => '2',
    '3' => '3', 'three' => '3',
    '4' => '4', 'four'  => '4',
    '5' => '5', 'five'  => '5',
    '6' => '6', 'six'   => '6',
    '7' => '7', 'seven' => '7',
    '8' => '8', 'eight' => '8',
    '9' => '9', 'nine'  => '9',
}

def part_one(data)
    data.map { |s| s.scan(/\d/).join.as { |d| d[0] + d[-1] } }.map(&:to_i).sum
end

def part_two(data)
    data.map do |s|
        DIGITS.map { |k, v| [s.index(k), v] }
            .filter { |a| a[0] }
            .min { |a, b| a[0] <=> b[0] }[1] + 
        DIGITS.map { |k, v| [s.reverse.index(k.reverse), v] }
            .filter { |a| a[0] }
            .min { |a, b| a[0] <=> b[0] }[1]
    end.map(&:to_i).sum
end

eg1_data = get_dataset(year: 2023, day: 1, type: 'example_1')
eg2_data = get_dataset(year: 2023, day: 1, type: 'example_2')
input_data = get_dataset(year: 2023, day: 1, type: 'input')

puts "Part One: #{part_one(eg1_data)}"
puts "Part Two: #{part_two(eg2_data)}"
puts "Part One: #{part_one(input_data)}"
puts "Part Two: #{part_two(input_data)}"
Output:
Part One: 142
Part Two: 281
Part One: 57346
Part Two: 57345