Skip to content

Instantly share code, notes, and snippets.

@zunda
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zunda/11371334 to your computer and use it in GitHub Desktop.
Save zunda/11371334 to your computer and use it in GitHub Desktop.
結城浩さん《スペーストーキー問題》 http://www.hyuki.com/codeiq/#c16 のデコーダ
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
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
@zunda
Copy link
Author

zunda commented Apr 28, 2014

https://gist.github.com/zunda/11371334/39472534145a35c5fe5b549b6239f88c5e8b9f75#file-decoder-rb-L8
L8に見落しがありました。前回26文字の場合には続けられるのですよね

@zunda
Copy link
Author

zunda commented Apr 28, 2014

https://gist.github.com/zunda/11371334/69eb70ce36f7d7a69dccc7dafee9657553c36685#file-decoder-rb-L10
というわけでフィードバックをいただいてからの改良版

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment