diff --git a/2023/day01.rb b/2023/day01.rb new file mode 100644 index 0000000..95a7b90 --- /dev/null +++ b/2023/day01.rb @@ -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 +) diff --git a/2023/examples/day01.txt b/2023/examples/day01.txt new file mode 100644 index 0000000..33f7317 --- /dev/null +++ b/2023/examples/day01.txt @@ -0,0 +1,7 @@ +two11nine +eightwo11three +abcone22threexyz +xtwone33four +4nineeightseven2 +zoneight234 +7pqr3stsixteen