オフラインでリアルタイムに「どう書く」をやるイベント。
またやります
http://atnd.org/events/31590
で。
改めて C++ で解いたので投稿。
問題は
http://nabetani.sakura.ne.jp/hena/ord2/
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iomanip>
struct test_data{
std::string m_input;
std::string m_expected;
};
const test_data test_data[]={
/* 0*/ {"ff-2f-23-f3-77-7f-3b", "1f-03-00-1c-0d-0f-06" },
/* 本当はあと 21個テストデータがある */
};
std::vector<int> string_to_bits( std::string const & s )
{
std::vector<int> r;
for( int i=0 ; i<s.size() ; i+=3 ){
r.push_back( strtol( s.c_str()+i, 0, 16 ) );
}
return r;
}
int and_all( std::vector<int> const & v )
{
int r=~0;
for( int a : v ){
r &= a;
}
return r;
}
std::string toString(std::vector<int> const & v )
{
std::stringstream s;
for( int a : v ){
s<<"-"<<std::setfill('0')<<std::hex<<std::setw(2)<<a;
}
return s.str().c_str()+1;
}
int kill_bits( int bits, int bits_to_kill )
{
int r=0;
for( int mask=0x80 ; mask ; mask/=2 ){
if ( !(bits_to_kill & mask ) ){
r<<=1;
r|=(bits&mask) ? 1 : 0;
}
}
return r;
}
std::string solve( std::string const & s )
{
std::vector<int> columns = string_to_bits( s );
int bits_to_kill = and_all( columns );
for( int & c : columns ){
c=kill_bits( c, bits_to_kill );
}
return toString(columns);
}
int main()
{
std::cout << "hello";
for( auto e : test_data ){
std::string actual = solve( e.m_input );
if ( actual != e.m_expected ){
std::cerr << "solv( " << e.m_input << " ) is " << actual << ", but expected is " << e.m_expected << ".\n";
}
}
return 0;
}
やや酔っぱらいつつ、若干 C++11 を利用して書いてみた。
テストデータをのぞくと70行ちょっと。まあそんなもんだろう。
まだまだ工夫改善の余地はあると思うけれどもこんなところで。