diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-12-08 20:14:20 +0200 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-12-08 20:28:31 +0200 |
| commit | d38030e86355e2357ac84349198b638bd74a495e (patch) | |
| tree | 70e75544036c91956d15a14b3b4ae9803f5cae07 /day04 | |
| parent | day03 (diff) | |
| download | aoc25-main.tar.gz aoc25-main.zip | |
Diffstat (limited to 'day04')
| -rw-r--r-- | day04/Makefile | 1 | ||||
| -rw-r--r-- | day04/solution.cpp | 82 | ||||
| -rw-r--r-- | day04/tests/test1.input | 10 | ||||
| -rw-r--r-- | day04/tests/test1.output | 2 |
4 files changed, 95 insertions, 0 deletions
diff --git a/day04/Makefile b/day04/Makefile new file mode 100644 index 0000000..2fa98c0 --- /dev/null +++ b/day04/Makefile | |||
| @@ -0,0 +1 @@ | |||
| include ../aoc.mk | |||
diff --git a/day04/solution.cpp b/day04/solution.cpp new file mode 100644 index 0000000..48afb58 --- /dev/null +++ b/day04/solution.cpp | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | #include <bits/stdc++.h> | ||
| 2 | |||
| 3 | namespace views = std::views; | ||
| 4 | namespace ranges = std::ranges; | ||
| 5 | |||
| 6 | void pad_grid(std::vector<std::string>& input) { | ||
| 7 | for (size_t i = 0; i < input.size(); ++i) { | ||
| 8 | input[i] = "." + input[i] + "."; | ||
| 9 | } | ||
| 10 | int const row_size = input[0].size(); | ||
| 11 | input.emplace(input.begin(), row_size, '.'); | ||
| 12 | input.emplace_back(row_size, '.'); | ||
| 13 | } | ||
| 14 | |||
| 15 | auto parse_input(std::istream& is) { | ||
| 16 | const std::string input = { | ||
| 17 | std::istreambuf_iterator<char>(is), | ||
| 18 | std::istreambuf_iterator<char>() | ||
| 19 | }; | ||
| 20 | |||
| 21 | auto lines = views::split(input, '\n') | ||
| 22 | | views::filter([](auto&& line) { return not line.empty(); }) | ||
| 23 | | ranges::to<std::vector<std::string>>(); | ||
| 24 | |||
| 25 | pad_grid(lines); | ||
| 26 | return lines; | ||
| 27 | } | ||
| 28 | |||
| 29 | constexpr std::array<char, 3> tuple_to_array(std::tuple<char, char, char>&& t) { | ||
| 30 | return {std::get<0>(t), std::get<1>(t), std::get<2>(t)}; | ||
| 31 | } | ||
| 32 | |||
| 33 | auto next_state(std::vector<std::string> const& input) { | ||
| 34 | int nremoved = 0; | ||
| 35 | auto forklift_pass = [&nremoved](auto&& columns) -> char { | ||
| 36 | if (columns[1][1] == '@' and ranges::count(columns | views::join, '@') < 5) { | ||
| 37 | ++nremoved; | ||
| 38 | return '.'; | ||
| 39 | } | ||
| 40 | return columns[1][1]; | ||
| 41 | }; | ||
| 42 | |||
| 43 | std::vector<std::string> next_state; | ||
| 44 | for (auto const& rows : input | views::slide(3)) { | ||
| 45 | next_state.push_back( | ||
| 46 | views::zip(rows[0], rows[1], rows[2]) | ||
| 47 | | views::transform(tuple_to_array) | ||
| 48 | | views::slide(3) | ||
| 49 | | views::transform(forklift_pass) | ||
| 50 | | ranges::to<std::string>()); | ||
| 51 | } | ||
| 52 | pad_grid(next_state); | ||
| 53 | return std::make_tuple(next_state, nremoved); | ||
| 54 | } | ||
| 55 | |||
| 56 | void part1(auto input) { | ||
| 57 | auto const [_, answer] = next_state(input); | ||
| 58 | std::println("{}", answer); | ||
| 59 | } | ||
| 60 | |||
| 61 | void part2(auto input) { | ||
| 62 | // Write second part solution here | ||
| 63 | int answer = 0; | ||
| 64 | while (true) { | ||
| 65 | auto const [_next_state, increment] = next_state(input); | ||
| 66 | if (increment == 0) { | ||
| 67 | break; | ||
| 68 | } | ||
| 69 | input = _next_state; | ||
| 70 | answer += increment; | ||
| 71 | } | ||
| 72 | std::println("{}", answer); | ||
| 73 | } | ||
| 74 | |||
| 75 | int main() { | ||
| 76 | auto input = parse_input(std::cin); | ||
| 77 | |||
| 78 | part1(input); | ||
| 79 | part2(input); | ||
| 80 | |||
| 81 | return 0; | ||
| 82 | } | ||
diff --git a/day04/tests/test1.input b/day04/tests/test1.input new file mode 100644 index 0000000..8209399 --- /dev/null +++ b/day04/tests/test1.input | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | ..@@.@@@@. | ||
| 2 | @@@.@.@.@@ | ||
| 3 | @@@@@.@.@@ | ||
| 4 | @.@@@@..@. | ||
| 5 | @@.@@@@.@@ | ||
| 6 | .@@@@@@@.@ | ||
| 7 | .@.@.@.@@@ | ||
| 8 | @.@@@.@@@@ | ||
| 9 | .@@@@@@@@. | ||
| 10 | @.@.@@@.@. | ||
diff --git a/day04/tests/test1.output b/day04/tests/test1.output new file mode 100644 index 0000000..a6118ef --- /dev/null +++ b/day04/tests/test1.output | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | 13 | ||
| 2 | 43 | ||
