summaryrefslogtreecommitdiffstats
path: root/day05
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2025-12-27 12:55:32 +0200
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2025-12-27 12:55:32 +0200
commitdc0eba6de4807dfc335c5ba31cdeff71e5eed717 (patch)
tree8289d3aa3a4df7af91846a5f6cd6e48634a77fe5 /day05
parentday04 (diff)
downloadaoc25-dc0eba6de4807dfc335c5ba31cdeff71e5eed717.tar.gz
aoc25-dc0eba6de4807dfc335c5ba31cdeff71e5eed717.zip
Diffstat (limited to 'day05')
-rw-r--r--day05/Makefile1
-rw-r--r--day05/solution.cpp84
-rw-r--r--day05/tests/test1.input11
-rw-r--r--day05/tests/test1.output2
4 files changed, 98 insertions, 0 deletions
diff --git a/day05/Makefile b/day05/Makefile
new file mode 100644
index 0000000..2fa98c0
--- /dev/null
+++ b/day05/Makefile
@@ -0,0 +1 @@
include ../aoc.mk
diff --git a/day05/solution.cpp b/day05/solution.cpp
new file mode 100644
index 0000000..fa4ff87
--- /dev/null
+++ b/day05/solution.cpp
@@ -0,0 +1,84 @@
1#include <bits/stdc++.h>
2
3namespace views = std::views;
4namespace ranges = std::ranges;
5using set = std::pair<uint64_t, uint64_t>;
6
7auto parse_input(std::istream& is) {
8 const std::string input = {
9 std::istreambuf_iterator<char>(is),
10 std::istreambuf_iterator<char>()
11 };
12
13 auto const lines = input
14 | views::split(std::string_view{"\n\n"})
15 | views::filter([](auto&& line) { return not line.empty(); })
16 | ranges::to<std::vector<std::string>>();
17
18 auto const sets = lines[0]
19 | views::split('\n')
20 | views::filter([](auto&& line) { return not line.empty(); })
21 | views::transform([](auto&& element) -> set {
22 auto const l = element | views::split('-') | ranges::to<std::vector<std::string>>();
23 return set{std::stoull(l[0]), std::stoull(l[1])};
24 })
25 | ranges::to<std::vector<set>>();
26
27 auto const numbers = lines[1]
28 | views::split('\n')
29 | views::filter([](auto&& line) { return not line.empty(); })
30 | views::transform([](auto&& elem) -> uint64_t { return std::stoull(elem | ranges::to<std::string>()); })
31 | ranges::to<std::vector>();
32
33 // Parse input
34 return std::make_tuple(sets, numbers);
35}
36
37void part1(auto const& input) {
38 // Write first part solution here
39 auto const& [sets, numbers] = input;
40 uint64_t answer = 0;
41 for (auto const n : numbers) {
42 answer += ranges::count_if(sets, [n](auto const& set) { return set.first <= n and n <= set.second; }) > 0;
43 }
44 std::println("{}", answer);
45}
46
47std::vector<set> merge_sets(std::vector<set> sets) {
48 ranges::sort(sets);
49
50 std::vector merged_sets = {sets[0]};
51 merged_sets.reserve(sets.size());
52
53 for (auto const& next : sets) {
54 auto& last = merged_sets.back();
55 if (last.second + 1 < next.first) {
56 // gap: [(1, 5), (7, 12)]
57 merged_sets.push_back(next);
58 } else {
59 // no gap: [(1, 5), (4, 8)]
60 last.second = std::max(last.second, next.second);
61 }
62 }
63
64 return merged_sets;
65}
66
67void part2(auto const& input) {
68 // Write second part solution here
69 auto const& [sets, numbers] = input;
70 auto const merged_sets = merge_sets(sets);
71 uint64_t answer = 0;
72 for (auto const set : merged_sets) {
73 answer += set.second - set.first + 1;
74 }
75 std::println("{}", answer);
76}
77
78int main() {
79 auto const input = parse_input(std::cin);
80 part1(input);
81 part2(input);
82
83 return 0;
84}
diff --git a/day05/tests/test1.input b/day05/tests/test1.input
new file mode 100644
index 0000000..2e9078d
--- /dev/null
+++ b/day05/tests/test1.input
@@ -0,0 +1,11 @@
13-5
210-14
316-20
412-18
5
61
75
88
911
1017
1132
diff --git a/day05/tests/test1.output b/day05/tests/test1.output
new file mode 100644
index 0000000..962c963
--- /dev/null
+++ b/day05/tests/test1.output
@@ -0,0 +1,2 @@
13
214