From 4fb0b2c46c5ebc5fc7fce438f5c8baf8e345c9e9 Mon Sep 17 00:00:00 2001 From: Thraix Date: Sun, 7 Dec 2025 14:20:48 +0100 Subject: [PATCH] Add solution for 2025 Day 7 --- res/2025/day07/test_input.txt | 16 ++++++++++ src/2025/Day07.cpp | 60 +++++++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 res/2025/day07/test_input.txt diff --git a/res/2025/day07/test_input.txt b/res/2025/day07/test_input.txt new file mode 100644 index 0000000..57a2466 --- /dev/null +++ b/res/2025/day07/test_input.txt @@ -0,0 +1,16 @@ +.......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... diff --git a/src/2025/Day07.cpp b/src/2025/Day07.cpp index 5b96394..0cb0446 100644 --- a/src/2025/Day07.cpp +++ b/src/2025/Day07.cpp @@ -2,35 +2,63 @@ namespace y2025::day07 { - using InputType = std::vector; - REGISTER_DAY(2025, Day07, InputType, int); + using InputType = Array2D; + REGISTER_DAY(2025, Day07, InputType, int64_t); - REGISTER_TEST_EXAMPLE(2025, Day07, ExampleInput, 1, 0); - REGISTER_TEST(2025, Day07, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2025, Day07, ExampleInput, 2, 0); - REGISTER_TEST(2025, Day07, Input, 2, 0); + REGISTER_TEST_EXAMPLE(2025, Day07, ExampleInput, 1, 21); + REGISTER_TEST(2025, Day07, Input, 1, 1594); + REGISTER_TEST_EXAMPLE(2025, Day07, ExampleInput, 2, 40); + REGISTER_TEST(2025, Day07, Input, 2, 15650261281478); READ_INPUT(input) { - std::vector vec; - std::string str; - while (std::getline(input, str)) + return Input::ReadArray2D(input); + } + + std::pair Simulate(const Array2D& grid) + { + std::pair solution{0, 1}; + + Index2D startPos = grid.Find('S'); + + std::vector beamsRow{}; + beamsRow.resize(grid.width); + beamsRow[startPos.x] = 1; + + for (int y = startPos.y; y < grid.height - 1; y++) { - std::stringstream ss{str}; - int n; - ss >> n; - vec.emplace_back(n); + std::vector nextBeamRow{}; + nextBeamRow.resize(grid.width); + + for (int x = 0; x < grid.width; x++) + { + if (beamsRow[x] == 0) + continue; + + if (grid.Get(x, y + 1) == '^') + { + nextBeamRow[x - 1] += beamsRow[x]; + nextBeamRow[x + 1] += beamsRow[x]; + solution.first++; + solution.second += beamsRow[x]; + } + else + { + nextBeamRow[x] += beamsRow[x]; + } + } + beamsRow = std::move(nextBeamRow); } - return vec; + return solution; } OUTPUT1(input) { - return 0; + return Simulate(input).first; } OUTPUT2(input) { - return 0; + return Simulate(input).second; } }