Skip to content

Instantly share code, notes, and snippets.

@safiire
Last active December 2, 2023 02:22
Show Gist options
  • Save safiire/ba2362f25b05af2f16a3371ee8ffbfba to your computer and use it in GitHub Desktop.
Save safiire/ba2362f25b05af2f16a3371ee8ffbfba to your computer and use it in GitHub Desktop.
Advent of Code 2023 Day 1
#!/usr/bin/env ruby
input = File.read('day_one.input')
Token = Data.define(:forward, :backward)
Tokens = {
eightwo: Token[8, 2],
eighthree: Token[8, 3],
fiveight: Token[5, 8],
nineight: Token[9, 8],
oneight: Token[1, 8],
sevenine: Token[7, 9],
threeight: Token[3, 8],
twone: Token[2, 1],
'1': Token[1, 1],
'2': Token[2, 2],
'3': Token[3, 3],
'4': Token[4, 4],
'5': Token[5, 5],
'6': Token[6, 6],
'7': Token[7, 7],
'8': Token[8, 8],
'9': Token[9, 9],
one: Token[1, 1],
two: Token[2, 2],
three: Token[3, 3],
four: Token[4, 4],
five: Token[5, 5],
six: Token[6, 6],
seven: Token[7, 7],
eight: Token[8, 8],
nine: Token[9, 9]
}
Regex = Regexp.new(Tokens.keys.join('|'))
result = input.split("\n").map do |line|
symbols = line.scan(Regex).map(&:to_sym)
tokens = symbols.map{ |symbol| Tokens[symbol] }
[tokens.first.forward, tokens.last.backward].join.to_i
end.sum
puts result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment