Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Last active April 4, 2017 12:37
Show Gist options
  • Save mizuhara/5a38d03640dc6507d36d76e28c03b55c to your computer and use it in GitHub Desktop.
Save mizuhara/5a38d03640dc6507d36d76e28c03b55c to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
#include <iostream>
class HexagonalTetromino
{
public:
HexagonalTetromino(
const std::string & name,
const std::string & src,
const std::vector<std::vector<int>> & diff)
:name_(name), src_(src), diff_(diff)
{}
bool exist_in(const std::string & field) const
{
const auto base = field.find(src_[0]);
if(base == std::string::npos) {
return false;
}
for(auto && d : diff_) {
std::vector<int> ps = { 0 };
for(std::size_t i = 1; i < src_.length(); ++i) {
ps.push_back(field.find(src_[i]) - base);
}
if(d == ps) {
return true;
}
}
return false;
}
std::string name() const { return name_; }
private:
const std::string name_;
const std::string src_;
const std::vector<std::vector<int>> diff_;
};
std::vector<HexagonalTetromino> make_hexagonal_tetrominos(const std::string & src)
{
std::vector<HexagonalTetromino> hts;
const std::vector<std::vector<int>> b = {
{ 0, 6, 7, 12 }, { 0, 4, 5, 6 },
{ 0, 5, 6, 12 }, { 0, 5, 10, 11 },
{ 0, 1, 6, 11 }, { 0, 1, 2, 6 }
};
hts.push_back(HexagonalTetromino("B", src, b));
const std::vector<std::vector<int>> d = {
{ 0, 6, 11, 12 }, { 0, 5, 6, 7 },
{ 0, 1, 2, 7 }, { 0, 4, 5, 10 },
{ 0, 1, 6, 12 }, { 0, 5, 6, 10 }
};
hts.push_back(HexagonalTetromino("D", src, d));
const std::vector<std::vector<int>> i = {
{ 0, 5, 10, 15 }, { 0, 1, 2, 3 },
{ 0, 6, 12, 18 }
};
hts.push_back(HexagonalTetromino("I", src, i));
const std::vector<std::vector<int>> j = {
{ 0, 5, 9, 10 }, { 0, 6, 7, 8 },
{ 0, 1, 2, 8 }, { 0, 1, 5, 10 },
{ 0, 5, 11, 17 }, { 0, 6, 12, 17 }
};
hts.push_back(HexagonalTetromino("J", src, j));
const std::vector<std::vector<int>> l = {
{ 0, 6, 12, 13 }, { 0, 1, 7, 13 },
{ 0, 3, 4, 5 }, { 0, 1, 2, 5 },
{ 0, 6, 11, 16 }, { 0, 5, 10, 16 }
};
hts.push_back(HexagonalTetromino("L", src, l));
const std::vector<std::vector<int>> n = {
{ 0, 1, 5, 7 }, { 0, 5, 11, 12 },
{ 0, 6, 10, 11 }, { 0, 1, 7, 12 },
{ 0, 1, 5, 11 }, { 0, 2, 6, 7 }
};
hts.push_back(HexagonalTetromino("N", src, n));
const std::vector<std::vector<int>> o = {
{ 0, 5, 6, 11 }, { 0, 1, 6, 7 },
{ 0, 1, 5, 6 }
};
hts.push_back(HexagonalTetromino("O", src, o));
const std::vector<std::vector<int>> s = {
{ 0, 5, 11, 16 }, { 0, 1, 4, 5 },
{ 0, 6, 7, 13 }
};
hts.push_back(HexagonalTetromino("S", src, s));
const std::vector<std::vector<int>> y = {
{ 0, 6, 7, 11 }, { 0, 4, 5, 11 },
{ 0, 6, 7, 13 }
};
hts.push_back(HexagonalTetromino("Y", src, y));
const std::vector<std::vector<int>> z = {
{ 0, 6, 11, 17 }, { 0, 1, 7, 8 },
{ 0, 4, 5, 9 }
};
hts.push_back(HexagonalTetromino("Z", src, z));
return hts;
}
std::string solve(const std::string & src)
{
const std::string field = "abcde*fghi*jklmn*opqr*stuvw";
const auto hts = make_hexagonal_tetrominos(src);
for(auto && ht : hts) {
if(ht.exist_in(field)) {
return ht.name();
}
}
return "-";
}
void test(const std::string & src, const std::string & expected)
{
const auto actual = solve(src);
std::cout << (actual == expected ? "ok" : "***NG***") << std::endl;
}
void test()
{
/*0*/ test( "glmq", "B" );
/*1*/ test( "fhoq", "-" );
/*2*/ test( "lmpr", "N" );
/*3*/ test( "glmp", "Y" );
/*4*/ test( "dhkl", "J" );
/*5*/ test( "glpq", "D" );
/*6*/ test( "hlmq", "O" );
/*7*/ test( "eimq", "I" );
/*8*/ test( "cglp", "S" );
/*9*/ test( "chlq", "Z" );
/*10*/ test( "glqr", "L" );
/*11*/ test( "cdef", "-" );
/*12*/ test( "hijk", "-" );
/*13*/ test( "kpqu", "B" );
/*14*/ test( "hklm", "B" );
/*15*/ test( "mqrw", "B" );
/*16*/ test( "nrvw", "B" );
/*17*/ test( "abfj", "B" );
/*18*/ test( "abcf", "B" );
/*19*/ test( "mrvw", "D" );
/*20*/ test( "ptuv", "D" );
/*21*/ test( "lmnr", "D" );
/*22*/ test( "hklp", "D" );
/*23*/ test( "himr", "D" );
/*24*/ test( "dhil", "D" );
/*25*/ test( "hlpt", "I" );
/*26*/ test( "stuv", "I" );
/*27*/ test( "bglq", "I" );
/*28*/ test( "glmn", "J" );
/*29*/ test( "fghm", "J" );
/*30*/ test( "cdgk", "J" );
/*31*/ test( "lpst", "J" );
/*32*/ test( "imrw", "J" );
/*33*/ test( "dinr", "J" );
/*34*/ test( "cdin", "L" );
/*35*/ test( "eghi", "L" );
/*36*/ test( "cdeg", "L" );
/*37*/ test( "bgko", "L" );
/*38*/ test( "eimr", "L" );
/*39*/ test( "jotu", "L" );
/*40*/ test( "kotu", "N" );
/*41*/ test( "lqtu", "N" );
/*42*/ test( "cdim", "N" );
/*43*/ test( "klot", "N" );
/*44*/ test( "kloq", "N" );
/*45*/ test( "kmpq", "N" );
/*46*/ test( "qrvw", "O" );
/*47*/ test( "mnqr", "O" );
/*48*/ test( "kopt", "O" );
/*49*/ test( "mnpq", "S" );
/*50*/ test( "bfko", "S" );
/*51*/ test( "chin", "S" );
/*52*/ test( "hmnq", "Y" );
/*53*/ test( "nqrw", "Y" );
/*54*/ test( "bchi", "Z" );
/*55*/ test( "inrw", "Z" );
/*56*/ test( "cfgj", "Z" );
/*57*/ test( "jnpv", "-" );
/*58*/ test( "flmp", "-" );
/*59*/ test( "adpw", "-" );
/*60*/ test( "eilr", "-" );
/*61*/ test( "bejv", "-" );
/*62*/ test( "enot", "-" );
/*63*/ test( "fghq", "-" );
/*64*/ test( "cjms", "-" );
/*65*/ test( "elov", "-" );
/*66*/ test( "chlm", "D" );
/*67*/ test( "acop", "-" );
/*68*/ test( "finr", "-" );
/*69*/ test( "qstu", "L" );
/*70*/ test( "abdq", "-" );
/*71*/ test( "jkln", "-" );
/*72*/ test( "fjkn", "-" );
/*73*/ test( "ijmn", "-" );
/*74*/ test( "flqr", "-" );
}
int main()
{
test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment