Skip to content

Commit

Permalink
Year 2023 Day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tudorpavel committed Oct 28, 2024
1 parent fbee56d commit 77e13e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2023/day01.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
lines = STDIN.read.split("\n")

puts (
lines.map do |l|
digits = l.scan(/\d/).flatten
(digits.first + digits.last).to_i
end.sum
)
DIGIT_MAP = {
'one' => '1',
'two' => '2',
'three' => '3',
'four' => '4',
'five' => '5',
'six' => '6',
'seven' => '7',
'eight' => '8',
'nine' => '9'
}
puts (
lines.map do |l|
digits = l.scan(/(?=(one|two|three|four|five|six|seven|eight|nine|\d))/).flatten
[digits.first, digits.last].map { |d| DIGIT_MAP[d] || d }.join.to_i
end.sum
)
7 changes: 7 additions & 0 deletions 2023/examples/day01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
two11nine
eightwo11three
abcone22threexyz
xtwone33four
4nineeightseven2
zoneight234
7pqr3stsixteen

0 comments on commit 77e13e7

Please sign in to comment.