summaryrefslogtreecommitdiffstats
path: root/day04/solution.cpp
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2025-12-08 20:14:20 +0200
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2025-12-08 20:28:31 +0200
commitd38030e86355e2357ac84349198b638bd74a495e (patch)
tree70e75544036c91956d15a14b3b4ae9803f5cae07 /day04/solution.cpp
parentday03 (diff)
downloadaoc25-d38030e86355e2357ac84349198b638bd74a495e.tar.gz
aoc25-d38030e86355e2357ac84349198b638bd74a495e.zip
Diffstat (limited to 'day04/solution.cpp')
-rw-r--r--day04/solution.cpp82
1 files changed, 82 insertions, 0 deletions
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
3namespace views = std::views;
4namespace ranges = std::ranges;
5
6void 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
15auto 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
29constexpr 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
33auto 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
56void part1(auto input) {
57 auto const [_, answer] = next_state(input);
58 std::println("{}", answer);
59}
60
61void 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
75int main() {
76 auto input = parse_input(std::cin);
77
78 part1(input);
79 part2(input);
80
81 return 0;
82}