seccon online

CTF雑魚なので、全然問題解けなかったのです。数毒のやつと、12問暗号のやつの11問までの部分点までしか解けなかった、という悲惨な結果です。

えっと、初めてのCTFということで初めてのWrite upを書いてみようと思いましたが、他の人にかなり完全な答えのWrite upがいくつかあったので、書きかけてたものを即消ししました。

精進します・・・

xcodeでboost使うためのメモ

非常に忘れっぽいので、というか毎回ググって誰かのブログから見てくるのがめんどくさいので、ここに書いておきます

例えば次のように関数を「nx^m」と書いたときのmが知りたいときってあるじゃないですか(?)。で、正規表現使いたいって思ってboostを読み込むわけですが、ちょっとめんどくさいので、書き残しておきます

regextest.h
#include <iostream>
#include <string>
#include <boost/regex.hpp>

using namespace std;
class RegexTest {
    
public:
    static string getDegree(string func_str);
};
regextest.mm
#include "regextest.h"

string RegexTest::getDegree(string func_str)
{
    boost::regex reg("(\\d*)x\\^(-?\\d+)");
    boost::smatch result;
    if (boost::regex_match(func_str, result, reg)) {
        return result.str(2);
    }
    return NULL;
    
}
main.mm
#import <Cocoa/Cocoa.h>
#include "regextest.h"

int main(int argc, char *argv[])
{
    string f("2x^3");
    cout << "("<< f << ")'s degree is " <<RegexTest::getDegree(f) << endl;
    return NSApplicationMain(argc, (const char **)argv);
}

これ普通にやろうとすると、リンクできないので、
f:id:moratorium08:20131106091148p:plain
このようにHeader Search Path(Build Settings の真ん中のあたりにSearch Pathという項目がありその中にあるやつ)を変更したあと、Build Phasesタブに移って、Link Binary With Librariesで、プラス押すとワーっとライブラリ押すといろいろ出てきますが、その中には無いので、左下のAdd Other押すと、ファイル全体のブラウジングできます。で、僕は/opt/local/lib/にいろいろあるので、そっから、今回使ったlibboost_regex-mt.aを選ぶと
f:id:moratorium08:20131106091613p:plain
実行してみると
f:id:moratorium08:20131106092800p:plain
できました。めでたしめでたし。(というか、まぁmain.mmで出来るので、他のところでもできますねってことですね)

あ、あといつもObjective-C++使うと忘れるのですが、main.mをmain.mmにするのを忘れるな と未来の自分に言っておきます。

Boost使って正規表現で名前付きキャプチャしようとした話

もともとPython

#coding:utf-8
from __future__ import print_function 
import re
s = "123hello"
# named capture 
m = re.match(r"(?P<num>\d*)(?P<str>.*)", s)
print("num is", m.group("num"))
print("str is", m.group("str"))

というように、正規表現を使って中の文字列に対して名前付きキャプチャしようとしただけなんですが、、、

以下C++で上のやつをやった結果です。

#include <iostream>
#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_algorithms.hpp>

using namespace boost::xpressive;
int main(void)
{
    std::string str("123hello");
    sregex rx = sregex::compile("(?P<num>\\d*)(?P<str>.*)");
    smatch m;

    if (regex_match(str.begin(), str.end(), m, rx)){
        std::cout << "num is " << m["num"] << std::endl;
        std::cout << "str is " << m["str"] << std::endl;
    }
}

User's Guide - 1.44.0
これ見れば一発かもしれませんが...

Pythonの子プロセスIO

すっごい見よう見まねですが。subprocessモジュール使って子プロセスのIOしてます

ipc_test_parent.py
#coding:utf-8
from subprocess import *
p = Popen("python ipc_test_child.py", shell=True,
                  stdin=PIPE, stdout=PIPE, stderr=STDOUT,  close_fds=True)
(child_stdin, child_stdout) = (p.stdin, p.stdout)

child_stdin.write("a\n")

while True:
    l = child_stdout.readline()
    if not l:
        break
    print l,
ipc_test_child.py
# coding:utf-8
a = raw_input()
print ''.join([a, "\t", "in child"])

http://d.hatena.ne.jp/kakurasan/20080413/p1

http://docs.python.jp/2.6/library/os.html#os.popen (公式んとこ)
参照した結果上記の謎コードが生まれました。

L-systemとな

http://www14.ocn.ne.jp/~kk62526/Lsys/
なんかすごいやつっぽい。遺伝的アルゴリズムでグラフの文法エンコードが使えるとか使えないとか

def lsystem(a):
    if a==0:
        return "F"
    str = lsystem(a-1)
    result=[]
    for x in str:
        if x=="F": result.append("F-F++F-F")
        else: result.append(x)
    return ''.join(result)

inp = input("step:")
print lsystem(inp)

Traveling Salesman Problem 2

昨日の巡回セールスマン問題何かがおかしいと思ってたら、案の定Processing側のコードがおかしかったわけです。

10地点(Simulated Annealing + Genetic Algorithm)
117世代0.00313452861531 が最大適応度
f:id:moratorium08:20131006105754p:plain

50地点(Simulated Annealing + Genetic Algorithm)
f:id:moratorium08:20131006105809p:plain

まだ50地点は最短距離ではないですが、ある程度はまとまって来てると思います。50地点の最短距離っぽいのができたら、とりあえず、一つの完成としたいですね。

50地点は、もう一度計算中です