Skip to content

Instantly share code, notes, and snippets.

@SaitoAtsushi
Created October 14, 2018 09:35
Show Gist options
  • Save SaitoAtsushi/31e8702616506bd796c68175ac249578 to your computer and use it in GitHub Desktop.
Save SaitoAtsushi/31e8702616506bd796c68175ac249578 to your computer and use it in GitHub Desktop.
プログラミングパズル的なアレ → http://nabetani.sakura.ne.jp/hena/ordf09rotbox/
OBJS = solver.o test.o
f90 : $(OBJS)
$(CXX) $(LDFLAGS) $^ -o $@
$(OBJS): %.o: %.cpp
$(CXX) -c $(CFLAGS) $< -o $@
clean:
rm -rf $(OBJS)
// -*- mode: c++ -*-
#include <algorithm>
#include <iostream>
#include <functional>
#include "solver.h"
using box = std::reference_wrapper<int>;
namespace std {
template<>
void swap<box>(box& lo, box& ro) noexcept {
swap((int&)lo, (int&)ro);
}
}
void panel_rotate(box (&a)[3]) {
std::rotate(std::begin(a), std::begin(a)+1, std::end(a));
}
solver::solver(char const* src) : matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} {
box pattern[][3] = {
{matrix[0][0], matrix[0][1], matrix[0][2]}, // a
{matrix[1][0], matrix[1][1], matrix[1][2]}, // b
{matrix[2][0], matrix[2][1], matrix[2][2]}, // c
{matrix[2][0], matrix[1][0], matrix[0][0]}, // d
{matrix[2][1], matrix[1][1], matrix[0][1]}, // e
{matrix[2][2], matrix[1][2], matrix[0][2]}, // f
{matrix[2][2], matrix[2][1], matrix[2][0]}, // g
{matrix[1][2], matrix[1][1], matrix[1][0]}, // h
{matrix[0][2], matrix[0][1], matrix[0][0]}, // i
{matrix[0][2], matrix[1][2], matrix[2][2]}, // j
{matrix[0][1], matrix[1][1], matrix[2][1]}, // k
{matrix[0][0], matrix[1][0], matrix[2][0]} // l
};
for(; *src!='\0'; src++) panel_rotate(pattern[*src-u'a']);
}
bool solver::operator==(char const* ro) {
for(int e: this->matrix[0]) if(*ro!='\0' && *ro++ != e+'0') return false;
if(*ro!='\0' && *ro++ != '/') return false;
for(int e: this->matrix[1]) if(*ro!='\0' && *ro++ != e+'0') return false;
if(*ro!='\0' && *ro++ != '/') return false;
for(int e: this->matrix[2]) if(*ro!='\0' && *ro++ != e+'0') return false;
if(*ro!='\0') return false;
return true;
}
std::ostream& operator<<(std::ostream& os, int (&a)[3]) {
for(auto e: a) os << e;
return os;
}
std::ostream& operator<<(std::ostream& os, solver& sol) {
os << sol.matrix[0] << '/' << sol.matrix[1] << '/' << sol.matrix[2];
return os;
}
// -*- mode: c++ -*-
class solver {
private:
int matrix[3][3];
public:
solver(char const* src);
bool operator==(char const* ro);
friend std::ostream& operator<<(std::ostream &, solver&);
};
// -*- mode: c++ -*-
#include <iostream>
#include "solver.h"
void test(const char* src, const char* expected) {
auto answer = solver(src);
auto okay = answer==expected;
std::cout << (okay ? "ok " : "**NG** ")
<< src << "->"
<< answer << std::endl;
}
int main(void) {
/*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");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment