diff --git a/res/2015/day18/test_input.txt b/res/2015/day18/test_input.txt index e69de29..6ddce72 100644 --- a/res/2015/day18/test_input.txt +++ b/res/2015/day18/test_input.txt @@ -0,0 +1,6 @@ +.#.#.# +...##. +#....# +..#... +#.#..# +####.. diff --git a/src/2015/Day18.cpp b/src/2015/Day18.cpp index 264e396..a664f9b 100644 --- a/src/2015/Day18.cpp +++ b/src/2015/Day18.cpp @@ -2,30 +2,113 @@ namespace y2015::day18 { - REGISTER_DAY(2015, Day18, std::vector, int); + REGISTER_DAY(2015, Day18, Array2D, int); - REGISTER_TEST_EXAMPLE(2015, Day18, ExampleInput, 1, 0); - REGISTER_TEST(2015, Day18, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2015, Day18, ExampleInput, 2, 0); - REGISTER_TEST(2015, Day18, Input, 2, 0); + REGISTER_TEST_EXAMPLE(2015, Day18, ExampleInput, 1, 4); + REGISTER_TEST(2015, Day18, Input, 1, 814); + REGISTER_TEST_EXAMPLE(2015, Day18, ExampleInput, 2, 7); + REGISTER_TEST(2015, Day18, Input, 2, 924); READ_INPUT(input) { - std::vector vec; - std::string str; - while (getline(input, str)) - { - } - return vec; + return Input::ReadArray2D(input); } OUTPUT1(input) { - return 0; + Array2D array1 = input; + Array2D array2 = input; + Array2D* current = &array1; + Array2D* other = &array2; + for (int i = 0; i < 100; i++) + { + current->Each( + [&other](const Array2D& array, Index2D index) + { + int neighbors = array.GetNeighbors('#', index.x, index.y, true); + if (array[index] == '#') + { + if (neighbors < 2 || neighbors > 3) + { + (*other)[index] = '.'; + } + else + { + (*other)[index] = '#'; + } + } + else if (array[index] == '.') + { + if (neighbors == 3) + { + (*other)[index] = '#'; + } + else + { + (*other)[index] = '.'; + } + } + }); + std::swap(current, other); + } + + return current->Count('#'); } OUTPUT2(input) { - return 0; + Array2D array1 = input; + Array2D array2 = input; + Array2D* current = &array1; + Array2D* other = &array2; + array1[Index2D{0, 0}] = '#'; + array2[Index2D{0, 0}] = '#'; + array1[Index2D{0, array1.height - 1}] = '#'; + array2[Index2D{0, array1.height - 1}] = '#'; + array1[Index2D{array1.width - 1, 0}] = '#'; + array2[Index2D{array1.width - 1, 0}] = '#'; + array1[Index2D{array1.width - 1, array1.height - 1}] = '#'; + array2[Index2D{array1.width - 1, array1.height - 1}] = '#'; + for (int i = 0; i < 100; i++) + { + current->Each( + [&other](const Array2D& array, Index2D index) + { + if (index.x == 0 && index.y == 0) + return; + if (index.x == 0 && index.y == array.height - 1) + return; + if (index.x == array.width - 1 && index.y == 0) + return; + if (index.x == array.width - 1 && index.y == array.height - 1) + return; + int neighbors = array.GetNeighbors('#', index.x, index.y, true); + if (array[index] == '#') + { + if (neighbors < 2 || neighbors > 3) + { + (*other)[index] = '.'; + } + else + { + (*other)[index] = '#'; + } + } + else if (array[index] == '.') + { + if (neighbors == 3) + { + (*other)[index] = '#'; + } + else + { + (*other)[index] = '.'; + } + } + }); + std::swap(current, other); + } + + return current->Count('#'); } }