Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Created February 3, 2018 00:25
Show Gist options
  • Save mizuhara/6ca3d0107e2bfff1e78d04440913dd7d to your computer and use it in GitHub Desktop.
Save mizuhara/6ca3d0107e2bfff1e78d04440913dd7d to your computer and use it in GitHub Desktop.
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
using matrix_t = std::vector<std::vector<int>>;
const std::string commands { "abcdefihglkj" };
matrix_t transpose(const matrix_t matrix)
{
auto m = matrix;
for(std::size_t i = 0; i < matrix.size(); ++i) {
for(std::size_t j = 0; j < matrix[i].size(); ++j) {
m[i][j] = matrix[j][i];
}
}
return m;
}
matrix_t shift(const matrix_t & matrix, char cmd)
{
auto m = matrix;
const auto p = commands.find(cmd) % matrix.size();
std::rotate(m[p].begin(), m[p].begin() + 1, m[p].end());
return m;
}
std::string format(const matrix_t & matrix)
{
std::stringstream ss;
for(std::size_t i = 0; i < matrix.size(); ++i) {
for(auto && e : matrix[i]) {
ss << e;
}
if(i + 1 < matrix.size()) {
ss << "/";
}
}
return ss.str();
}
std::string solve(const std::string & src)
{
matrix_t matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
};
for(auto && chr : src) {
if('a' <= chr && chr <= 'c') {
matrix = shift(matrix, chr);
} else if('d' <= chr && chr <= 'f') {
auto m = transpose(matrix);
m = shift(shift(m, chr), chr);
matrix = transpose(m);
} else if('g' <= chr && chr <= 'i') {
matrix = shift(shift(matrix, chr), chr);
} else {
auto m = transpose(matrix);
m = shift(m, chr);
matrix = transpose(m);
}
}
return format(matrix);
}
void test(const std::string & src, const std::string & expected)
{
const auto actual = solve(src);
if(actual != expected) {
std::cout << "FAILED: [src=" << src << "]" << " [actual=" << actual << "]" << " [expected=" << expected << "]" << std::endl;
}
}
void test_all()
{
/*0*/ test("aegj", "286/435/971");
/*1*/ test("a", "231/456/789");
/*2*/ test("e", "183/426/759");
/*3*/ test("g", "123/456/978");
/*4*/ test("j", "126/459/783");
/*5*/ test("bb", "123/645/789");
/*6*/ test("jjj", "123/456/789");
/*7*/ test("bd", "723/164/589");
/*8*/ test("ah", "231/645/789");
/*9*/ test("bj", "124/569/783");
/*10*/ test("db", "723/561/489");
/*11*/ test("dh", "723/615/489");
/*12*/ test("dl", "123/456/789");
/*13*/ test("hc", "123/645/897");
/*14*/ test("gf", "128/453/976");
/*15*/ test("hl", "623/745/189");
/*16*/ test("ja", "261/459/783");
/*17*/ test("ld", "123/456/789");
/*18*/ test("ki", "315/486/729");
/*19*/ test("lfa", "294/753/186");
/*20*/ test("kga", "531/486/972");
/*21*/ test("dbi", "372/561/489");
/*22*/ test("che", "193/625/847");
/*23*/ test("iea", "823/416/759");
/*24*/ test("gbl", "523/964/178");
/*25*/ test("egj", "186/425/973");
/*26*/ test("jcf", "127/456/839");
/*27*/ test("djh", "726/915/483");
/*28*/ test("hld", "123/645/789");
/*29*/ test("leeh", "453/678/129");
/*30*/ test("heja", "851/629/743");
/*31*/ test("cakh", "251/649/837");
/*32*/ test("bhjik", "652/489/713");
/*33*/ test("eabji", "483/269/751");
/*34*/ test("cdbch", "823/156/974");
/*35*/ test("ckgajc", "536/492/817");
/*36*/ test("ggchha", "231/564/978");
/*37*/ test("gfbkeg", "128/534/697");
/*38*/ test("agfbcbf", "239/148/765");
/*39*/ test("ekahijf", "123/645/789");
/*40*/ test("hajdjbe", "789/432/615");
/*41*/ test("elgililj", "976/325/814");
/*42*/ test("chffefif", "317/629/845");
/*43*/ test("ilbbihak", "462/587/319");
/*44*/ test("abcdefghijkl", "123/456/789");
/*45*/ test("hkijbglfaced", "768/125/493");
/*46*/ test("dfkbjiechlga", "256/387/419");
/*47*/ test("hgfkbidlajce", "186/745/239");
/*48*/ test("baciefjhgkdl", "153/482/796");
}
int main()
{
test_all();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment