The Python Challenge on Ruby 3

http://www.pythonchallenge.com/

Rubyで解く。激しくネタばれ注意。

レベル4

http://www.pythonchallenge.com/pc/def/linkedlist.php
について、

つまりは、これの繰り返しということだ。


require 'open-uri'

base_url = 'http://www.pythonchallenge.com/pc/def/'
arg = '12345'
while true
file_name = 'linkedlist.php?nothing=' + arg
arg = open(base_url + file_name){|f|
body = f.read
puts body
body.gsub(/and the next nothing is (.*)/,'\1')
}
end

無限ループの最低なスクリプトだが、このクイズの特性上、馬鹿正直なパターンマッチングだけで最後まですんなり行くとは思えない。

1番目の罠

and the next nothing is 92512
and the next nothing is 64505
<font color=red>Your hands are getting tired </font>and the next nothing is 50010
URI::InvalidURIError: bad URI(is not URI?): http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=<font color=red>Your hands are getting tired </font>50010
        from /usr/lib/ruby/1.8/uri/common.rb:432:in `split'
        from /usr/lib/ruby/1.8/uri/common.rb:481:in `parse'
        from /usr/lib/ruby/1.8/open-uri.rb:85:in `open'
        from (irb):33
        from :0

やはり罠。ちょっとした変化をつけてきた。
これに対応できるよう10行目を

    body.gsub(/(.*)and the next nothing is (.*)/,'\2')

として再実行。

2番目の罠

and the next nothing is 92512
and the next nothing is 64505
<font color=red>Your hands are getting tired </font>and the next nothing is 50010
and the next nothing is 29193
and the next nothing is 89924
and the next nothing is 65471
and the next nothing is 69977
and the next nothing is 6306
and the next nothing is 16460
and the next nothing is 15222
and the next nothing is 48564
and the next nothing is 7359
and the next nothing is 53915
and the next nothing is 26258
and the next nothing is 55683
and the next nothing is 37852
and the next nothing is 61066
There maybe misleading numbers in the
text. One example is 61067. Look only for the next nothing and the next nothing is 53522
Errno::ENOENT: No such file or directory - http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=There maybe misleading numbers in the
53522
        from /usr/lib/ruby/1.8/open-uri.rb:88:in `initialize'
        from /usr/lib/ruby/1.8/open-uri.rb:88:in `open'
        from (irb):20
        from :0

今度は2行に分けてきた。
ここらでスクリプトを見直し。


require 'open-uri'

base_url = 'http://www.pythonchallenge.com/pc/def/'
arg = '12345'
file_name = ''
while arg != nil
file_name = 'linkedlist.php?nothing=' + arg
body = URI(base_url + file_name).read
p body
arg = body.delete("\n").gsub!(/(.*)and the next nothing is (.*)/,'\2')
end
print file_name

これで再々実行。

さらに罠

"and the next nothing is 92118"
"Yes. Divide by two and keep going."
linkedlist.php?nothing=92118

文章で割り算を指示? いよいよ先が見えてきた。


require 'open-uri'

base_url = 'http://www.pythonchallenge.com/pc/def/'
arg = '12345'
file_name = ''
while arg != nil
file_name = 'linkedlist.php?nothing=' + arg
body = URI(base_url + file_name).read
p body
if body == "Yes. Divide by two and keep going."
arg = (arg.to_i/2).to_s
next
end
arg = body.delete("\n").gsub!(/(.*)and the next nothing is (.*)/,'\2')
end
print file_name

"Yes. Divide by two and keep going."
"and the next nothing is 33110"
"and the next nothing is 17675"
"and the next nothing is 8511"
"and the next nothing is 89456"
"and the next nothing is 43502"
"and the next nothing is 45605"
"and the next nothing is 12970"
"and the next nothing is 91060"
"and the next nothing is 27719"
"and the next nothing is 65667"
"peak.html"
linkedlist.php?nothing=65667

212行の出力を吐き出した末に、ようやく終わった。
解答は"peak.html"だ。