Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Created February 14, 2015 02:48
Show Gist options
  • Save mizuhara/714a313655955bacee7b to your computer and use it in GitHub Desktop.
Save mizuhara/714a313655955bacee7b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include <boost/lexical_cast.hpp>
using namespace std;
vector<string> split(string const& src, string const& separator)
{
vector<string> v;
const regex reg = separator.empty() ? regex("(.{1})") : regex(separator);
const int frag = separator.empty() ? 1 : -1;
copy(sregex_token_iterator(src.begin(), src.end(), reg, frag), sregex_token_iterator(), back_inserter(v));
return v;
}
vector<long> init(string const& src)
{
const vector<string> v = split(src, ",");
vector<long> ret;
for(int i = 0; i < 12; ++i) {
switch(i) {
case 0:
case 2:
ret.push_back(boost::lexical_cast<long>(v[1]));
break;
case 3:
case 5:
ret.push_back(boost::lexical_cast<long>(v[2]));
break;
case 6:
case 8:
ret.push_back(boost::lexical_cast<long>(v[3]));
break;
case 9:
ret.push_back(boost::lexical_cast<long>(v[0]));
break;
case 11:
ret.push_back(boost::lexical_cast<long>(v[0]) - 1);
break;
default:
ret.push_back(2);
break;
}
}
return ret;
}
string index_to_direction(int index)
{
switch(index) {
case 1:
case 3:
case 11:
return "S";
case 2:
case 4:
case 6:
return "W";
case 5:
case 7:
case 9:
return "N";
default:
return "E";
}
}
void update_wall(vector<long> & wall, int update_count)
{
for(size_t i = 0; i < wall.size(); ++i) {
switch(i) {
case 0:
if(update_count == 0) {
wall[i] += 1;
}
break;
case 1:
case 4:
case 7:
case 10:
wall[i] += 2;
break;
default:
// do nothing.
break;
}
}
}
string solve(string const& src)
{
const vector<string> terms = split(src, ":");
vector<long> wall = init(terms[0]);
long days = boost::lexical_cast<long>(terms[1]);
int index = 0, update_count = 0;
string direction = index_to_direction(index);
while(true) {
days -= wall[index];
if(days < 0) {
break;
}
direction = index_to_direction(++index);
if(days == 0) {
if(wall[index] == 0) {
direction = index_to_direction(index + 1);
}
break;
}
if(index == 12) {
update_wall(wall, update_count++);
index = 0;
direction = index_to_direction(index);
}
}
return direction;
}
void test(string const& src, string const& expected)
{
static int test_number = 0;
const string actual = solve(src);
if(actual != expected) {
cout
<< test_number
<< ":NG"
<< " act: " << actual << ", exp: " << expected << endl;
}
++test_number;
}
void test_all()
{
/*0*/ test( "2,3,5,4:85", "S" );
/*1*/ test( "1,2,3,4:1", "E" );
/*2*/ test( "1,2,3,4:2", "S" );
/*3*/ test( "1,2,3,4:3", "S" );
/*4*/ test( "1,2,3,4:4", "W" );
/*5*/ test( "1,2,3,4:27", "E" );
/*6*/ test( "1,2,3,4:62", "E" );
/*7*/ test( "1,2,3,4:40", "W" );
/*8*/ test( "1,4,3,2:40", "S" );
/*9*/ test( "3,3,3,3:30", "S" );
/*10*/ test( "3,3,3,3:31", "E" );
/*11*/ test( "3,3,3,3:32", "E" );
/*12*/ test( "3,3,3,3:70", "S" );
/*13*/ test( "3,3,3,3:71", "E" );
/*14*/ test( "3,3,3,3:72", "E" );
/*15*/ test( "1,1,1,1:7", "N" );
/*16*/ test( "1,2,1,1:7", "W" );
/*17*/ test( "1,6,1,1:7", "S" );
/*18*/ test( "1,8,1,1:7", "E" );
/*19*/ test( "1,1,1,1:30", "N" );
/*20*/ test( "1,2,1,1:30", "W" );
/*21*/ test( "1,5,1,1:30", "S" );
/*22*/ test( "1,8,1,1:30", "E" );
/*23*/ test( "9,9,9,9:99", "W" );
/*24*/ test( "5,6,3,8:3", "E" );
/*25*/ test( "5,8,1,1:11", "W" );
/*26*/ test( "2,8,1,2:18", "S" );
/*27*/ test( "3,2,3,1:20", "N" );
/*28*/ test( "3,3,8,1:28", "N" );
/*29*/ test( "2,5,1,2:32", "E" );
/*30*/ test( "2,5,1,6:33", "E" );
/*31*/ test( "1,2,5,7:34", "N" );
/*32*/ test( "3,6,5,6:36", "E" );
/*33*/ test( "6,2,8,1:39", "S" );
/*34*/ test( "3,1,2,3:41", "W" );
/*35*/ test( "1,1,3,4:45", "W" );
/*36*/ test( "1,3,1,2:46", "N" );
/*37*/ test( "4,4,4,4:49", "W" );
/*38*/ test( "3,1,4,4:55", "N" );
/*39*/ test( "6,6,2,1:56", "W" );
/*40*/ test( "3,2,1,2:59", "S" );
/*41*/ test( "2,7,7,1:60", "S" );
/*42*/ test( "3,1,1,1:63", "N" );
/*43*/ test( "4,6,4,1:78", "E" );
/*44*/ test( "7,5,3,6:79", "W" );
/*45*/ test( "7,8,3,1:81", "E" );
/*46*/ test( "3,2,5,2:82", "S" );
/*47*/ test( "1,1,3,4:84", "N" );
/*48*/ test( "7,4,1,5:88", "S" );
/*49*/ test( "3,6,5,3:89", "S" );
/*50*/ test( "1,4,2,3:92", "N" );
/*51*/ test( "1,3,4,5:93", "W" );
/*52*/ test( "2,4,8,1:94", "W" );
/*53*/ test( "3,6,1,7:99", "S" );
// 追加問題
test( "1234,2345,3456,4567:978593417", "E" );
test( "1234,2345,3456,4567:978593418", "S" );
test( "31415,92653,58979,32384:9812336139", "W" );
test( "31415,92653,58979,32384:9812336140", "S" );
test( "314159,265358,979323,84626:89099331642", "S" );
test( "314159,265358,979323,84626:89099331643", "W" );
}
int main()
{
test_all();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment