summaryrefslogtreecommitdiffstats
path: root/day02
diff options
context:
space:
mode:
Diffstat (limited to 'day02')
-rw-r--r--day02/Makefile1
-rw-r--r--day02/solution.cpp85
-rw-r--r--day02/tests/test1.input3
-rw-r--r--day02/tests/test1.output2
4 files changed, 91 insertions, 0 deletions
diff --git a/day02/Makefile b/day02/Makefile
new file mode 100644
index 0000000..2fa98c0
--- /dev/null
+++ b/day02/Makefile
@@ -0,0 +1 @@
include ../aoc.mk
diff --git a/day02/solution.cpp b/day02/solution.cpp
new file mode 100644
index 0000000..23304c1
--- /dev/null
+++ b/day02/solution.cpp
@@ -0,0 +1,85 @@
1#include <bits/stdc++.h>
2
3namespace views = std::views;
4namespace ranges = std::ranges;
5
6auto parse_input(std::istream& is) {
7 const std::string input = {
8 std::istreambuf_iterator<char>(is),
9 std::istreambuf_iterator<char>()
10 };
11
12 auto char_range_to_int = [](auto&& r) {
13 auto&& s = ranges::to<std::string>(r);
14 return std::stoll(s);
15 };
16 auto lines = views::split(input, ',')
17 | views::filter([](auto line) { return not line.empty(); });
18
19 std::vector<std::pair<long, long>> _ranges;
20 for (auto const line : lines) {
21 auto numbers = line
22 | views::split('-')
23 | views::transform(char_range_to_int);
24 _ranges.emplace_back(*numbers.begin(), *std::next(numbers.begin()));
25 }
26 return _ranges;
27}
28
29
30void part1(auto const& input) {
31 // Write first part solution here
32 long answer = 0;
33
34 for (auto [begin, end] : input) {
35 for (auto i = begin; i <= end; ++i) {
36 std::string s{std::format("{}", i)};
37 size_t half_size = s.size() / 2;
38 std::string_view sl{s.begin(), s.begin() + half_size};
39 std::string_view sr{s.begin() + half_size, s.end()};
40 if (sl == sr) {
41 answer += i;
42 }
43 }
44 }
45
46 std::println("{}", answer);
47}
48
49void part2(auto const& input) {
50 // Write second part solution here
51 long answer = 0;
52
53 for (auto [begin, end] : input) {
54 for (auto i = begin; i <= end; ++i) {
55 std::stringstream ss;
56 ss << i;
57 std::string_view s{ss.str()};
58 for (int chunk_size = 1; chunk_size <= s.size() / 2; ++chunk_size) {
59 if (s.size() % chunk_size != 0) {
60 continue;
61 }
62 auto chunks = s | views::chunk(chunk_size);
63 auto value = *chunks.begin();
64 if (ranges::all_of(chunks, [value](auto&& chunk) {
65 return ranges::equal(chunk, value);
66 })) {
67 answer += i;
68 break;
69 }
70 }
71 }
72 }
73
74 std::println("{}", answer);
75
76}
77
78int main() {
79 auto const input = parse_input(std::cin);
80
81 part1(input);
82 part2(input);
83
84 return 0;
85}
diff --git a/day02/tests/test1.input b/day02/tests/test1.input
new file mode 100644
index 0000000..b3dc7c4
--- /dev/null
+++ b/day02/tests/test1.input
@@ -0,0 +1,3 @@
111-22,95-115,998-1012,1188511880-1188511890,222220-222224,
21698522-1698528,446443-446449,38593856-38593862,565653-565659,
3824824821-824824827,2121212118-2121212124 \ No newline at end of file
diff --git a/day02/tests/test1.output b/day02/tests/test1.output
new file mode 100644
index 0000000..0ab4cb7
--- /dev/null
+++ b/day02/tests/test1.output
@@ -0,0 +1,2 @@
11227775554
24174379265