Last active
January 2, 2016 23:59
-
-
Save syntacticsugar/8380225 to your computer and use it in GitHub Desktop.
project euler 17. took me foarever!!!!! and I had to get help from Nicky Bicky!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. | |
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? | |
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage. | |
=end | |
# "and" COUNTS! | |
# "and" comes only after "hundred". 101 - 999 | |
one = [""] + %w( one two three four five six seven eight nine ) | |
teen = %w( nil eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen ) | |
ten = %w( nil ten twenty thirty forty fifty sixty seventy eighty ninety ) | |
class String | |
def letter_count | |
self.split(" ").join.length | |
end | |
end | |
one_to_19 = one[1..-1] + ["ten"] + teen[1..-1] | |
# one of the most brainless things I've ever done: | |
# twenty_to_ninety_nine = ten[2] + 2.upto(9).map { |x| ten[2]+one[x] } | |
twenty_to_99 = ten[2..-1].flat_map do | |
|t| one.map do |w| | |
t + " " + w | |
end | |
end | |
#twenty_to_ninety_nine = ten[2..-1] + ten[2..-1].flat_map do | |
# |t| one[1..-1].map do |w| | |
# t + " " + w | |
# end | |
#end | |
one_to_99 = one_to_19 + twenty_to_99 | |
one_to_999 = one_to_99 + one[1..-1].map { |o| o + " hundred" } + one[1..-1].flat_map do |w| | |
one_to_99.map do |w2| | |
w + "hundred and" + w2 | |
end | |
end | |
one_to_1000 = one_to_999 + ["one thousand"] | |
numbers = one_to_1000 | |
array = numbers.map { |x| x.letter_count } | |
p array.inject(:+) | |
# answer: 21124 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is Bil's stuff, wow!!!!! :