-
-
Save mattsan/6511934d510363322b95 to your computer and use it in GitHub Desktop.
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
// compile: | |
// $ g++ -std=c++11 -o esm_doukaku_20151022 esm_doukaku_20151022.cpp | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
unsigned int char2bits(char c) | |
{ | |
unsigned int bits[] = { 0x00u, 0x80u, 0xc0u, 0xe0u, 0xf0u, 0xf8u, 0xfcu, 0xfeu, 0xffu }; | |
return bits[c - '0']; | |
} | |
unsigned int rotate_right(unsigned int n, int r) | |
{ | |
return ((n << (8 - r)) & 0xffu) | ((n >> r) & 0xffu); | |
} | |
std::string solve(const std::string& input) | |
{ | |
unsigned int waiting = 0; | |
unsigned int eating = 0; | |
unsigned int resting = 0; | |
auto i = std::begin(input); | |
auto end = std::end(input); | |
while(i != end) | |
{ | |
resting = eating; | |
eating = waiting; | |
waiting = 0; | |
for(int r = 0; r < 8; ++r) | |
{ | |
auto visitors = rotate_right(char2bits(*i), r); | |
if(((eating | resting) & visitors) == 0) | |
{ | |
waiting = visitors; | |
++i; | |
break; | |
} | |
} | |
} | |
auto sitting = waiting | eating | resting; | |
std::ostringstream oss; | |
for(int i = 0; i < 8; ++i) | |
{ | |
oss << ((sitting >> (7 - i)) & 1u); | |
} | |
return oss.str(); | |
} | |
void test(const std::string& input, const std::string& expected) | |
{ | |
auto actual = solve(input); | |
if(actual == expected) | |
{ | |
std::cout << '.'; | |
} | |
else | |
{ | |
std::cout | |
<< "\nFAILURE\n" | |
<< "input: " << input | |
<< ", expected: " << expected | |
<< ", actual: " << actual | |
<< std::endl; | |
} | |
} | |
int main(int, char* []) | |
{ | |
test("3316", "11111110"); | |
test("3342153", "11111111"); | |
test("8", "11111111"); | |
test("232624", "11110110"); | |
test("1", "10000000"); | |
test("224673432", "11111000"); | |
test("123464334", "11111110"); | |
test("44372332", "11111111"); | |
test("2344", "11111111"); | |
test("6448476233", "11111100"); | |
test("4345174644", "11111111"); | |
test("77743", "11111110"); | |
test("2136524281", "10000000"); | |
test("344373", "11100000"); | |
test("434226", "11111111"); | |
test("7433223", "11111110"); | |
test("2246534", "11110111"); | |
test("634", "11111110"); | |
test("2222", "11111100"); | |
test("2442343238", "11111111"); | |
test("7243344", "11111111"); | |
test("26147234", "10111111"); | |
test("34424", "10011111"); | |
test("6334", "11011111"); | |
test("3828342", "11011110"); | |
test("4431", "11110000"); | |
test("2843212125", "11111111"); | |
test("333336482", "11000000"); | |
test("374", "11110000"); | |
test("4382333", "11100111"); | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment