Last active
March 23, 2017 21:49
-
-
Save mizuhara/0704594bd582c710646d7ebf6b12848e to your computer and use it in GitHub Desktop.
An answer of http://nabetani.sakura.ne.jp/hena/ordf03triom/
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
#include <regex> | |
#include <string> | |
#include <vector> | |
#include <numeric> | |
#include <iostream> | |
#include "boost/algorithm/string.hpp" | |
class Triomino | |
{ | |
public: | |
Triomino(const char name, const std::regex & pattern) | |
:name_(name), pattern_(pattern) | |
{} | |
bool exist_in(const std::string & field) const | |
{ | |
return std::regex_search(field, pattern_); | |
} | |
char name() const { return name_; } | |
private: | |
const char name_; | |
const std::regex pattern_; | |
}; | |
std::string make_field(const std::string & src) | |
{ | |
const std::vector<std::string> v = { | |
"zzzzzz", | |
"abcde", | |
"fghij", | |
"klmno", | |
"pqrst", | |
"uvwxy", | |
"zzzzzz", | |
}; | |
std::string field = boost::algorithm::join(v, "zz"); | |
for(auto && chr : src) { | |
const auto pos = field.find(chr); | |
if(pos != std::string::npos) { | |
field.replace(pos, 1, "#"); | |
} | |
} | |
return field; | |
} | |
char solve(const std::string & src) | |
{ | |
const std::string field = make_field(src); | |
const std::vector<Triomino> triominos = { | |
{ 'J', std::regex("#[a-z]{5}#{2}") }, | |
{ 'L', std::regex("#[a-z]{6}#{2}") }, | |
{ 'T', std::regex("#{2}[a-z]{6}#") }, | |
{ 'R', std::regex("#{2}[a-z]{5}#") }, | |
{ 'I', std::regex("#[a-z]{6}#[a-z]{6}#") }, | |
{ 'B', std::regex("#{3}") }, | |
}; | |
for(auto && triomino : triominos) { | |
if(triomino.exist_in(field)) { | |
return triomino.name(); | |
} | |
} | |
return '-'; | |
} | |
void test(const std::string & src, const char expected) | |
{ | |
const auto actual = solve(src); | |
std::cout << (actual == expected ? "ok" : "***NG***") << std::endl; | |
} | |
int main() | |
{ | |
/*0*/ test( "cba", 'B' ); | |
/*1*/ test( "yam", '-' ); | |
/*2*/ test( "aaa", '-' ); | |
/*3*/ test( "def", '-' ); | |
/*4*/ test( "gga", '-' ); | |
/*5*/ test( "bbf", '-' ); | |
/*6*/ test( "gmh", 'T' ); | |
/*7*/ test( "mhn", 'L' ); | |
/*8*/ test( "dea", '-' ); | |
/*9*/ test( "mrn", 'R' ); | |
/*10*/ test( "hcm", 'I' ); | |
/*11*/ test( "mno", 'B' ); | |
/*12*/ test( "snr", 'J' ); | |
/*13*/ test( "xnn", '-' ); | |
/*14*/ test( "nnl", '-' ); | |
/*15*/ test( "kop", '-' ); | |
/*16*/ test( "ejd", 'T' ); | |
/*17*/ test( "txy", 'J' ); | |
/*18*/ test( "pvu", 'L' ); | |
/*19*/ test( "baf", 'R' ); | |
/*20*/ test( "hhc", '-' ); | |
/*21*/ test( "ono", '-' ); | |
/*22*/ test( "wxv", 'B' ); | |
/*23*/ test( "bdc", 'B' ); | |
/*24*/ test( "ojt", 'I' ); | |
/*25*/ test( "fkp", 'I' ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment