From c32f5a380ed736663d61f0535877d3cff9537b09 Mon Sep 17 00:00:00 2001 From: Thraix Date: Thu, 4 Dec 2025 20:44:51 +0100 Subject: [PATCH] Add solution for 2025 Day 4 --- res/2025/day04/test_input.txt | 10 ++++++ src/2025/Day04.cpp | 61 +++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 res/2025/day04/test_input.txt diff --git a/res/2025/day04/test_input.txt b/res/2025/day04/test_input.txt new file mode 100644 index 0000000..8209399 --- /dev/null +++ b/res/2025/day04/test_input.txt @@ -0,0 +1,10 @@ +..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@. diff --git a/src/2025/Day04.cpp b/src/2025/Day04.cpp index 71cf8fa..c45c2d6 100644 --- a/src/2025/Day04.cpp +++ b/src/2025/Day04.cpp @@ -2,35 +2,62 @@ namespace y2025::day04 { - using InputType = std::vector; + using InputType = Array2D; REGISTER_DAY(2025, Day04, InputType, int); - REGISTER_TEST_EXAMPLE(2025, Day04, ExampleInput, 1, 0); - REGISTER_TEST(2025, Day04, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2025, Day04, ExampleInput, 2, 0); - REGISTER_TEST(2025, Day04, Input, 2, 0); + REGISTER_TEST_EXAMPLE(2025, Day04, ExampleInput, 1, 13); + REGISTER_TEST(2025, Day04, Input, 1, 1502); + REGISTER_TEST_EXAMPLE(2025, Day04, ExampleInput, 2, 43); + REGISTER_TEST(2025, Day04, Input, 2, 9083); READ_INPUT(input) { - std::vector vec; - std::string str; - while (std::getline(input, str)) - { - std::stringstream ss{str}; - int n; - ss >> n; - vec.emplace_back(n); - } - return vec; + return Input::ReadArray2D(input); } OUTPUT1(input) { - return 0; + std::set toiletPapers; + for (auto it = input.begin(); it != input.end(); it++) + { + if (input[it.index] != '@' || input.GetNeighbors('@', it.index.x, it.index.y, 1) >= 4) + continue; + + toiletPapers.emplace(it.index); + } + return toiletPapers.size(); } OUTPUT2(input) { - return 0; + Array2D cpy = input; + + std::stack toCheck; + for (auto it = cpy.begin(); it != cpy.end(); it++) + { + if (cpy[it.index] == '.' || cpy.GetNeighbors('@', it.index.x, it.index.y, 1) >= 4) + continue; + + toCheck.emplace(it.index); + } + + int removedToiletPapers = 0; + while (!toCheck.empty()) + { + Index2D top = toCheck.top(); + toCheck.pop(); + if (cpy[top] == '.' || cpy.GetNeighbors('@', top.x, top.y, 1) >= 4) + continue; + + cpy[top] = '.'; + removedToiletPapers++; + std::vector neighbors = cpy.GetNeighbors(top, true); + for (auto neighbor : neighbors) + { + if (cpy[neighbor] == '@') + toCheck.emplace(neighbor); + } + } + return removedToiletPapers; } }