Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Created April 20, 2017 22:15
Show Gist options
  • Save mizuhara/8ef674378a1479ae2cfbe3bfcda9a095 to your computer and use it in GitHub Desktop.
Save mizuhara/8ef674378a1479ae2cfbe3bfcda9a095 to your computer and use it in GitHub Desktop.
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>
const std::vector<int> lines = {
128, 64, 32, 16, 8, 4, 2, 1,
};
template <typename Iterator> auto join(Iterator first, Iterator last, const std::string & sep)
{
std::stringstream ss;
ss << *first;
while(++first != last) {
ss << sep << *first;
}
return ss.str();
}
auto calc_polygon_impl(int x, int y)
{
auto dist = 1;
const int max = static_cast<int>(lines.size());
while(x != y) {
++x;
++dist;
if(max <= x) {
x = 0;
}
}
return (dist == 5) ? dist : dist + 1;
}
auto calc_polygon(const std::vector<int> & src)
{
std::vector<int> res;
auto x = *std::begin(src);
for(auto it = std::next(std::begin(src)), end = std::end(src); it != end; ++it) {
const auto y = *it;
auto dist = calc_polygon_impl(x, y);
res.push_back(dist);
x = y;
}
std::sort(std::begin(res), std::end(res));
return res;
}
auto calc_line(const std::string & src)
{
std::vector<int> res;
auto n = std::stoi(src);
for(int i = 0; n != 0; ++i) {
const auto current = lines[i];
if(current <= n) {
n -= current;
res.push_back(i);
}
}
res.push_back(res[0]);
return res;
}
std::string solve(const std::string & src)
{
const auto lines = calc_line(src);
const auto polygons = calc_polygon(lines);
return join(std::begin(polygons), std::end(polygons), "");
}
void test(const std::string & src, const std::string & expected)
{
const auto actual = solve(src);
if(actual == expected) {
std::cout << "ok" << std::endl;
} else {
std::cout << "ng: actual=" << actual << ", expected=" << expected << std::endl;
}
}
int main()
{
/*0*/ test("165", "3445");
/*1*/ test("80", "48");
/*2*/ test("255", "33333333");
/*3*/ test("68", "55");
/*4*/ test("200", "355");
/*5*/ test("82", "455");
/*6*/ test("164", "455");
/*7*/ test("73", "455");
/*8*/ test("146", "455");
/*9*/ test("37", "455");
/*10*/ test("74", "455");
/*11*/ test("148", "455");
/*12*/ test("41", "455");
/*13*/ test("38", "355");
/*14*/ test("76", "355");
/*15*/ test("152", "355");
/*16*/ test("49", "355");
/*17*/ test("98", "355");
/*18*/ test("196", "355");
/*19*/ test("137", "355");
/*20*/ test("19", "355");
/*21*/ test("20", "48");
/*22*/ test("9", "57");
/*23*/ test("209", "3345");
/*24*/ test("121", "33345");
/*25*/ test("239", "3333334");
/*26*/ test("26", "347");
/*27*/ test("111", "333344");
/*28*/ test("95", "333344");
/*29*/ test("85", "4444");
/*30*/ test("24", "39");
/*31*/ test("97", "347");
/*32*/ test("234", "33444");
/*33*/ test("59", "33345");
/*34*/ test("187", "333344");
/*35*/ test("34", "55");
/*36*/ test("249", "333335");
/*37*/ test("43", "3445");
/*38*/ test("143", "33335");
/*39*/ test("28", "338");
/*40*/ test("79", "33345");
/*41*/ test("173", "33444");
/*42*/ test("55", "33345");
/*43*/ test("77", "3445");
/*44*/ test("35", "355");
/*45*/ test("153", "3355");
/*46*/ test("30", "3337");
/*47*/ test("228", "3355");
/*48*/ test("177", "3345");
/*49*/ test("162", "445");
/*50*/ test("184", "3345");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment