[2023][ruby][1.*]

main
Alpha Chen 11 months ago
parent 5c2f1eb45f
commit 37ac8ad85b
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -0,0 +1,4 @@
source "https://rubygems.org"
gem "minitest"
gem "rake"

@ -0,0 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
minitest (5.20.0)
rake (13.1.0)
PLATFORMS
arm64-darwin-22
DEPENDENCIES
minitest
rake
BUNDLED WITH
2.4.1

@ -0,0 +1,24 @@
lines = ARGF.readlines(chomp: true)
# part one
p lines
.map { _1.chars.select {|c| c =~ /\d/ }}
.map { _1.values_at(0, -1) }
.sum { _1.join.to_i }
# part two
spellings = %w[
one two three four five six seven eight nine
].each.with_index.to_h { [_1, _2 + 1] }
digits = (spellings.keys + spellings.values).join(?|)
first_re = /(#{digits})(?~#{digits})/
last_re = /.*(#{digits})/
p lines
.sum { |line|
[first_re, last_re]
.map { _1.match(line)[1] }
.map { spellings.fetch(_1, &:to_i) }
.join
.to_i
}
Loading…
Cancel
Save