Google DevFest 2010 DevQuiz

今更だけど例のあれ。動くことは動くというレベル。


暗号化のやつ。String::trを使えばいいと後で知った。

#!/usr/bin/ruby

require "net/http"
require "uri"

def enc(c)
  e = 0
  if 65 <= c && c <= 90 then
    e = c + 16
    if e > 90 then
      e -= 26
    end
  elsif 97 <= c && c <= 122 then
    e = c + 16
    if e > 122 then
      e -= 26
    end
  else 
    e = c
  end
  return e
end

def encrypt(target)
  output = []
  target.each_byte do |c|
    output.push(c)
  end
  i=0
  while i < output.size do
    c = output[i]
    output[i] = enc(c).chr
    i+=1
  end
  return output.join("")
end

key = "1d05agdkZXZxdWl6chwLEhRQYXJ0aWNpcGFudFNvbHV0aW9ucxj7xgEM"
pass = encrypt("yourmailaddress@gmail.com")

body = "{\"key\": \"#{key}\", \"pass\": \"#{pass}\"}"
#puts body

uri = URI.parse("http://devquiz.appspot.com/personalpost")
Net::HTTP.start(uri.host, uri.port){|http|
  response = http.post(uri.path, body)
}

パッチワークのやつ。ぷよぷよと同じような探索の仕方。もっとスマートな方法がありそう。

#!/usr/bin/ruby

class MyData
  def initialize
    @data=[]
    @column_size=0
    @row_size=0
    @max=0
    @result=[]
  end

  def read
    $stdin.each do |line|
      line.chomp!
      column=[]
      line.each_byte do |c|
        column.push(c.chr)
      end
      if @column_size == 0 then
        @column_size = column.size
      elsif column.size != @column_size then
        puts "column size error!"
      end
      @data.push(column)
      @result.push(0)
    end
    @row_size = @data.size
    if @column_size != @row_size then
      puts "row size error!"
    end
  end

  def show
    @result.each do |num|
      puts num
    end
  end

  def search(i,j)
    type=@data[i][j]
    @tmp=[]
    n=search2(i,j,type)
    @tmp.each do |x,y|
      @data[x][y]=n
    end
    if n > @max then
      @max = n
    end
  end

  def search2(i,j,type)
    n=1
    @data[i][j] = "T"
    @tmp.push([i,j])
    if i+1 < @row_size && @data[i+1][j] == type then
      n += search2(i+1,j,type) 
    end
    if j+1 < @column_size && @data[i][j+1] == type then
      n += search2(i,j+1,type) 
    end
    if i-1 >= 0 && @data[i-1][j] == type then
      n += search2(i-1,j,type) 
    end
    if j-1 >= 0 && @data[i][j-1] == type then
      n += search2(i,j-1,type)
    end
    return n
  end

  def eval 
    i=0
    while i < @data.size do
      j=0
      while j < @data[i].size do
        c = @data[i][j]
        if c == "A" || c == "B" then
          search(i,j)
        end
        j+=1
      end
      i+=1
    end

    i=0
    while i < @data.size do
      j=0
      while j < @data[i].size do
        if @data[i][j] == @max then
          @result[i] += 1
        end
        j+=1
      end
      i+=1
    end
  end
end

test=MyData.new
test.read
test.eval
test.show

漢数字変換はサーバが用意できなかったのでチャレンジしなかった。App Engineでやろうかと思ったけど、環境を再構築する時間がなかったので。考え方としては、入力の桁数の上限が決まっているので数字を文字列にして左側をゼロ埋めし、4文字ずつ切り出して加工するといったところか。

私の点数は15点で参加資格が得られなかった。一方で漢数字変換を解いて22点の人はクリアしたようなので、このあたりがボーダーラインだったようだ。