Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Last active May 27, 2017 12:05
Show Gist options
  • Save mizuhara/ed727f1a3af2e4afe17962d9c44b6d85 to your computer and use it in GitHub Desktop.
Save mizuhara/ed727f1a3af2e4afe17962d9c44b6d85 to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
#include <iomanip>
#include <iostream>
#include "boost/range/adaptors.hpp"
#include "boost/algorithm/string.hpp"
const int invalid_pos = -1;
const std::string empty_string = "0";
const std::string block_string = "1";
std::vector<std::string> split(const std::string & src, const std::string & delim)
{
std::vector<std::string> ret;
boost::split(ret, src, boost::is_any_of(delim));
return ret;
}
std::vector<std::string> chunk(const std::string & src, std::size_t length)
{
std::vector<std::string> ret;
for(std::size_t i = 0; i < src.size(); i += length) {
ret.push_back(boost::adaptors::copy(src, i, i + length));
}
return ret;
}
struct Input
{
std::string center;
std::string block;
};
Input parse(const std::string & src)
{
const auto center_and_block = split(src, ":");
return { center_and_block[0], center_and_block[1] };
}
std::vector<int> create_rotate_table(const std::string & center)
{
if(center.compare("a") == 0) {
std::vector<int> ret = {
8, 13, 18, 23, invalid_pos,
7, 12, 17, 22, invalid_pos,
6, 11, 16, 21, invalid_pos,
5, 10, 15, 20, invalid_pos,
invalid_pos, invalid_pos, invalid_pos, invalid_pos, invalid_pos,
};
return ret;
}
if(center.compare("b") == 0) {
std::vector<int> ret = {
3, 8, 13, 18, 23,
2, 7, 12, 17, 22,
1, 6, 11, 16, 21,
0, 5, 10, 15, 20,
invalid_pos, invalid_pos, invalid_pos, invalid_pos, invalid_pos,
};
return ret;
}
return std::vector<int>();
}
std::string solve(const std::string & src)
{
const auto in = parse(src);
const auto rot_table = create_rotate_table(in.center);
const auto block = boost::algorithm::join(split(in.block, "/"), "");
struct mass {
const std::size_t w = 5;
const std::size_t h = 5;
} m;
std::string ret(m.w * m.h, '0');
for(std::size_t i = 0; i < block.length(); ++i) {
if(block.substr(i, 1).compare(empty_string) == 0) {
continue;
}
const auto p = rot_table[i];
if(p == invalid_pos) {
return "-";
}
ret.replace(p, 1, block_string);
}
return boost::algorithm::join(chunk(ret, m.w), "/");
}
void test(const std::string & src, const std::string & expected)
{
static int no = 0;
const auto actual = solve(src);
std::cout << std::setfill('0') << std::setw(2) << no;
if(actual == expected) {
std::cout << ": ok";
} else {
std::cout << ": NG |" << " actual=" << actual << ", expected=" << expected;
}
std::cout << std::endl;
++no;
}
void test_all()
{
/*0*/ test("a:00000/00110/00100/00100/00000", "00000/00000/00000/11100/00100");
/*1*/ test("b:00000/00000/00000/00011/00011", "-");
/*2*/ test("a:00000/00000/00000/00011/00011", "-");
/*3*/ test("b:00000/00000/00100/00000/00000", "00000/00000/01000/00000/00000");
/*4*/ test("a:00000/00000/00100/00000/00000", "00000/00000/00000/01000/00000");
/*5*/ test("b:00000/00110/00100/00100/00000", "00000/00000/11100/00100/00000");
/*6*/ test("b:00000/00000/00011/00011/00000", "00000/00000/00000/11000/11000");
/*7*/ test("a:00000/00000/00011/00011/00000", "-");
/*8*/ test("a:01110/00100/00000/00000/00000", "00000/00000/00010/00110/00010");
/*9*/ test("b:01110/00100/00000/00000/00000", "00000/00010/00110/00010/00000");
/*10*/ test("a:00000/11110/00000/00000/00000", "00000/00100/00100/00100/00100");
/*11*/ test("b:00000/11110/00000/00000/00000", "00100/00100/00100/00100/00000");
/*12*/ test("a:00000/00011/00110/00000/00000", "-");
/*13*/ test("b:00000/00011/00110/00000/00000", "00000/00000/01000/01100/00100");
/*14*/ test("a:00000/11100/11100/11100/00000", "00000/11100/11100/11100/00000");
/*15*/ test("b:00000/11100/11100/11100/00000", "11100/11100/11100/00000/00000");
/*16*/ test("a:01000/00000/00101/10010/10001", "-");
/*17*/ test("b:01000/00000/00101/10010/10001", "-");
/*18*/ test("b:10000/00000/10010/00000/00000", "01010/00000/00000/01000/00000");
/*19*/ test("a:10000/00000/10010/00000/00000", "00000/01010/00000/00000/01000");
/*20*/ test("a:00000/10101/11010/11010/01000", "-");
/*21*/ test("b:00000/10101/11010/11010/01000", "-");
/*22*/ test("b:01101/00011/01101/00000/00000", "00000/01010/01010/00100/01110");
/*23*/ test("a:01101/00011/01101/00000/00000", "-");
/*24*/ test("a:00001/00000/00000/00100/00010", "-");
/*25*/ test("b:00001/00000/00000/00100/00010", "-");
/*26*/ test("b:00100/00000/00100/01000/00000", "00000/10000/01010/00000/00000");
/*27*/ test("a:00100/00000/00100/01000/00000", "00000/00000/10000/01010/00000");
/*28*/ test("a:00010/00100/00000/10000/00000", "00000/10000/00000/00100/00010");
/*29*/ test("b:00010/00100/00000/10000/00000", "10000/00000/00100/00010/00000");
/*30*/ test("b:11010/00011/10101/00001/00001", "-");
/*31*/ test("a:11010/00011/10101/00001/00001", "-");
/*32*/ test("a:00100/00010/00000/11000/00000", "00000/10000/10000/00010/00100");
/*33*/ test("b:00100/00010/00000/11000/00000", "10000/10000/00010/00100/00000");
/*34*/ test("b:01010/00000/00000/01000/00000", "00000/10010/00000/00010/00000");
/*35*/ test("a:01010/00000/00000/01000/00000", "00000/00000/10010/00000/00010");
/*36*/ test("a:00000/00000/00100/10100/00000", "00000/10000/00000/11000/00000");
/*37*/ test("b:00000/00000/00100/10100/00000", "10000/00000/11000/00000/00000");
/*38*/ test("b:10000/01101/01000/01100/10011", "-");
/*39*/ test("a:10000/01101/01000/01100/10011", "-");
/*40*/ test("a:00010/00000/00110/01000/10001", "-");
/*41*/ test("b:00010/00000/00110/01000/10001", "-");
/*42*/ test("b:00000/01000/01100/00000/00000", "00000/01100/01000/00000/00000");
/*43*/ test("a:00000/01000/01100/00000/00000", "00000/00000/01100/01000/00000");
/*44*/ test("a:01000/00000/00000/10000/00000", "00000/10000/00010/00000/00000");
/*45*/ test("b:01000/00000/00000/10000/00000", "10000/00010/00000/00000/00000");
/*46*/ test("b:00000/01101/00000/01010/11010", "-");
/*47*/ test("a:00000/01101/00000/01010/11010", "-");
/*48*/ test("a:00110/00101/00000/10100/00100", "-");
/*49*/ test("b:00110/00101/00000/10100/00100", "-");
/*50*/ test("b:11000/10110/00000/00110/00000", "00110/00010/10100/10100/00000");
/*51*/ test("a:11000/10110/00000/00110/00000", "00000/00110/00010/10100/10100");
/*52*/ test("a:00000/00000/00000/00001/00110", "-");
/*53*/ test("b:00000/00000/00000/00001/00110", "-");
/*54*/ test("b:01011/10001/00000/00000/00000", "00100/00010/00000/00010/00110");
/*55*/ test("a:01011/10001/00000/00000/00000", "-");
}
int main()
{
test_all();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment