diff --git a/res/2016/day09/test_input.txt b/res/2016/day09/test_input.txt index e69de29..3e22f39 100644 --- a/res/2016/day09/test_input.txt +++ b/res/2016/day09/test_input.txt @@ -0,0 +1 @@ +X(8x2)(3x3)ABCY diff --git a/src/2016/Day09.cpp b/src/2016/Day09.cpp index 87f4912..32fe32d 100644 --- a/src/2016/Day09.cpp +++ b/src/2016/Day09.cpp @@ -2,30 +2,74 @@ namespace y2016::day09 { - REGISTER_DAY(2016, Day09, std::vector, int); + REGISTER_DAY(2016, Day09, std::string, uint64_t); - REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 1, 0); - REGISTER_TEST(2016, Day09, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 2, 0); - REGISTER_TEST(2016, Day09, Input, 2, 0); + REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 1, 18); + REGISTER_TEST(2016, Day09, Input, 1, 99145); + REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 2, 20); + REGISTER_TEST(2016, Day09, Input, 2, 10943094568); READ_INPUT(input) { - std::vector vec; std::string str; - while (getline(input, str)) + getline(input, str); + return str; + } + + std::pair 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 vec; + return count; } 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) { - return 0; + return GetLength(input.c_str(), input.size()); } }