Last active
August 29, 2015 14:00
-
-
Save ustreamer-01647/4d096ec1e04d1effa29b to your computer and use it in GitHub Desktop.
スペーストーキー社の危機を救え!
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<?php | |
function encode($input) { | |
// 空文字列の場合と非[a-z]を含む場合 | |
$match = preg_match("/[^a-z]/", $input); | |
if ($match === FALSE || $match === 1) { | |
return "?"; | |
} | |
// 文字列長制限 | |
if (strlen($input) > 1024) { | |
return "?"; | |
} | |
// TODO: エンコード | |
} | |
// 文字個数を調べるためのオフセット値 | |
define("ORDOFFSET", ord("a") - 1); | |
/** | |
* "ac" -> "aaa". "ax" -> "aaaaaaaaaaaaaaaaaaaaaaaa" | |
* abcdefghijklmnopqrstuvwx | |
* @param type $input 2字 | |
* @return string エンコードするとコマンドの2字になる文字列 | |
*/ | |
function decode($input) { | |
$count = ord($input[1]) - ORDOFFSET; | |
// 最初の文字を取り出し,count数だけ最初の文字で埋める | |
return str_pad($input[0], $count, $input[0]); | |
} | |
/** | |
* | |
* @param type $input コマンド | |
* @return string エンコードするとコマンドになる文字列 | |
*/ | |
function answer($input) { | |
// 空文字列の場合と非[a-z]を含む場合 | |
$match = preg_match("/[^a-z]/", $input); | |
if ($match === FALSE || $match === 1) { | |
return "X"; | |
} | |
// 文字列長が奇数である場合 | |
$length = strlen($input); | |
if ($length % 2 === 1) { | |
return "X"; | |
} | |
$command = ""; | |
// 2字ずつ処理する | |
for ($i = 0; $i < $length; $i += 2) { | |
$command .= decode(substr($input, $i, 2)); | |
} | |
// 文字列長制限 | |
if (strlen($command) > 1024) { | |
return "X"; | |
} | |
return $command; | |
} | |
// 改行コード単位で配列に読み込み | |
$words = explode("\r\n", file_get_contents("words.txt")); | |
// 作業する.元データを書き換えるから"&"付加 | |
foreach ($words as &$word) { | |
if (strlen($word) > 0) { | |
$word = answer($word) . ":" . $word; | |
} | |
} | |
// 改行コードで連結しつつファイル出力 | |
file_put_contents("result.txt", implode("\r\n", $words)); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment