Last active
August 29, 2015 14:00
-
-
Save zunda/11371334 to your computer and use it in GitHub Desktop.
結城浩さん《スペーストーキー問題》 http://www.hyuki.com/codeiq/#c16 のデコーダ
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
require 'open-uri' | |
ARGF.each_line do |l| | |
input, output = l.chomp.split(/:/) | |
next if input =~ /[A-Z]/ | |
actual = open("http://spacetalky.textfile.org/api.cgi?input=#{input}").read | |
if output != actual | |
puts "From #{input} expected #{output} but actually #{actual}" | |
end | |
sleep(0.5) | |
end |
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
class EncodedString < String | |
@@zero = 'a'.ord - 1 | |
def decode | |
return 'X' unless self.strip.length.even? | |
r = '' | |
p = '' | |
self.strip.scan(/(.)(.)/).each do |c, n| | |
return 'X' if p == c # 同じ文字が続いてしまう | |
r += c * (n.ord - @@zero) | |
p = n != 'z' ? c : '' # 前回の長さが'z'の場合は同じ文字が続いてもよい | |
end | |
return r | |
end | |
end | |
ARGF.each_line do |l| | |
puts "#{EncodedString.new(l).decode}:#{l.chomp}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/zunda/11371334/69eb70ce36f7d7a69dccc7dafee9657553c36685#file-decoder-rb-L10
というわけでフィードバックをいただいてからの改良版