挑戦

ラマ僧


挑戦してみた。

#!/usr/bin/ruby -Ke

MIN_LENGTH = 7
MAX_LENGTH = 9

class String
  def over_repeated?
    return false unless self[-1] == self[-2]
    return false unless self[-1] == self[-3]
    return true
  end

  def short_length?
    return size < MIN_LENGTH
  end

  def over_length?
    return size > MAX_LENGTH
  end
end

class Lama
  def main
    @file = open("result.log", "w")
    @count = 0
    @start_time = Time.now
    generate("")
    @end_time = Time.now
    result
  end

  def generate(string)
    ?a.upto(?z) do |char|
      new_string = string + char.chr
      break if new_string.over_length?
      next if new_string.over_repeated?
      unless new_string.short_length?
        @file.puts sprintf("%10d: #{new_string}", @count += 1)
      end
      generate(new_string)
    end
  end
  
  def result
    time_distance = @end_time - @start_time
    time_distance_hour = (time_distance / (60 * 60)).to_i
    time_distance_min = ((time_distance % (60 * 60)) / 60).to_i
    time_distance_sec = (time_distance % 60).to_i
    
    stamp_start = "開始時刻: #{@start_time.strftime("%Y-%m-%d %H:%M:%S")}"
    stamp_end = "終了時刻: #{@end_time.strftime("%Y-%m-%d %H:%M:%S")}"
    stamp_distance = ""
    stamp_distance += "所要時間:"
    stamp_distance += " #{time_distance_hour} 時間" if time_distance_hour > 0
    stamp_distance += " #{time_distance_min}" if time_distance_min > 0
    stamp_distance += " #{time_distance_sec}"

    @file.puts
    @file.puts stamp_start
    @file.puts stamp_end
    @file.puts stamp_distance
    @file.puts

    puts stamp_start
    puts stamp_end
    puts stamp_distance
  end
end

Lama.new.main

追記

むむ。どこかに GC.start を入れとかないとマズいかも。
リモートで放っておいたら刺さってるっぽい。