Created
March 29, 2017 14:53
-
-
Save shouhei/157fd4ffd2b55e519794045d7b5f1e84 to your computer and use it in GitHub Desktop.
プロジェクトオイラー17
This file contains 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
# http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2017 | |
base_one = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] | |
base_ten = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] | |
base_hundred = "hundred" | |
base_thousand = "thousand" | |
exceptions = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] | |
base = base_one + base_ten + exceptions + base_ten.slice(1..-1).product(base_one).map { |set| set[0] ++ set[1] } | |
punctuation = base_one.map{ |b| b + base_hundred } | |
middle = base + punctuation.product(base).map { |set| set[0] + "and"+ set[1] } | |
puts middle.map { |m| m.length }.inject(:+) + punctuation.map{ |m| m.length }.inject(:+) + ("one" + base_thousand).length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment