Add solution for 2016 Day 9
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
X(8x2)(3x3)ABCY
|
||||||
|
|||||||
+55
-11
@@ -2,30 +2,74 @@
|
|||||||
|
|
||||||
namespace y2016::day09
|
namespace y2016::day09
|
||||||
{
|
{
|
||||||
REGISTER_DAY(2016, Day09, std::vector<int>, int);
|
REGISTER_DAY(2016, Day09, std::string, uint64_t);
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 1, 0);
|
REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 1, 18);
|
||||||
REGISTER_TEST(2016, Day09, Input, 1, 0);
|
REGISTER_TEST(2016, Day09, Input, 1, 99145);
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 2, 0);
|
REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 2, 20);
|
||||||
REGISTER_TEST(2016, Day09, Input, 2, 0);
|
REGISTER_TEST(2016, Day09, Input, 2, 10943094568);
|
||||||
|
|
||||||
READ_INPUT(input)
|
READ_INPUT(input)
|
||||||
{
|
{
|
||||||
std::vector<int> vec;
|
|
||||||
std::string str;
|
std::string str;
|
||||||
while (getline(input, str))
|
getline(input, str);
|
||||||
{
|
return str;
|
||||||
}
|
}
|
||||||
return vec;
|
|
||||||
|
std::pair<int, int> GetMarker(const char* str)
|
||||||
|
{
|
||||||
|
char* end;
|
||||||
|
int length = std::strtol(str, &end, 10);
|
||||||
|
int repeat = std::strtol(end + 1, nullptr, 10);
|
||||||
|
return {length, repeat};
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t GetLength(const char* str, size_t strLength)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
uint64_t count = 0;
|
||||||
|
while (i < strLength)
|
||||||
|
{
|
||||||
|
if (str[i] == '(')
|
||||||
|
{
|
||||||
|
size_t close = std::strstr(str + i, ")") - str;
|
||||||
|
auto [length, repeat] = GetMarker(str + i + 1);
|
||||||
|
count += GetLength(str + close + 1, length) * repeat;
|
||||||
|
i = close + 1 + length;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT1(input)
|
OUTPUT1(input)
|
||||||
{
|
{
|
||||||
return 0;
|
uint64_t count = 0;
|
||||||
|
size_t i = 0;
|
||||||
|
while (i < input.size())
|
||||||
|
{
|
||||||
|
if (input[i] == '(')
|
||||||
|
{
|
||||||
|
size_t close = input.find(')', i);
|
||||||
|
auto [length, repeat] = GetMarker(input.c_str() + i + 1);
|
||||||
|
count += length * repeat;
|
||||||
|
i = close + 1 + length;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT2(input)
|
OUTPUT2(input)
|
||||||
{
|
{
|
||||||
return 0;
|
return GetLength(input.c_str(), input.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user