Skip to content

Instantly share code, notes, and snippets.

@syntacticsugar
Last active January 2, 2016 23:59
Show Gist options
  • Save syntacticsugar/8380225 to your computer and use it in GitHub Desktop.
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!
=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
@syntacticsugar
Copy link
Author

this is Bil's stuff, wow!!!!! :

=begin
NUMBER LETTER COUNTS
Problem 17
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

class Integer
  ONES = %w[ zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen ]
  TENS = %w[ zero ten twenty thirty forty fifty sixty seventy eighty ninety ]

  def to_words
    case self
    when 0..19
      ONES[self]
    when 20..99
      tens, ones = self.divmod 10
      "#{TENS[tens]}#{"-#{ones.to_words}" unless ones.zero?}"
    when 100..999
      hundreds, ones = self.divmod 100
      "#{hundreds.to_words} hundred#{" and #{ones.to_words}" unless ones.zero?}"
    when 1000
      "one thousand"
    else
      raise "I can't be bothered to do this for _all_ numbers, you annoying twonk!"
    end
  end
end

1.upto(1000) {|i| puts i.to_words }

num_letters = 1.upto(1000).reduce(0) {|count, i| count + i.to_words.gsub(/[ -]/, '').length }

puts
puts "Number of letters is #{num_letters}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment