summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2025-08-27 04:18:43 +0300
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2025-09-01 04:51:37 +0300
commite0879b1fc2608befa6eee4dd8c2a82ade94e94ce (patch)
tree5de27d72fa3131477b0b08815a8b663b47f022b4
parentAdd ‘skel/‘ as template directory for each day (diff)
downloadaoc24-e0879b1fc2608befa6eee4dd8c2a82ade94e94ce.tar.gz
aoc24-e0879b1fc2608befa6eee4dd8c2a82ade94e94ce.zip
day01/: rewrite solution to be in line with the template
-rw-r--r--day01/Makefile14
-rw-r--r--day01/solution.cpp53
-rw-r--r--day01/tests/test1.input6
-rw-r--r--day01/tests/test1.output2
4 files changed, 44 insertions, 31 deletions
diff --git a/day01/Makefile b/day01/Makefile
index c1bc66d..2fa98c0 100644
--- a/day01/Makefile
+++ b/day01/Makefile
@@ -1,13 +1 @@
1CXX := g++ include ../aoc.mk
2CXX_FLAGS := -std=c++23
3SOLUTION := solution.cpp
4INPUT := input.txt
5TARGET := solution.out
6
7all: compile run
8
9compile:
10 ${CXX} ${CXX_FLAGS} ${SOLUTION} -o ${TARGET}
11
12run: compile
13 ./${TARGET} < ${INPUT}
diff --git a/day01/solution.cpp b/day01/solution.cpp
index b19504e..78535a3 100644
--- a/day01/solution.cpp
+++ b/day01/solution.cpp
@@ -4,10 +4,28 @@
4#include <algorithm> 4#include <algorithm>
5#include <ranges> 5#include <ranges>
6#include <map> 6#include <map>
7#include <print>
8#include <iostream>
9#include <utility>
10
11namespace ranges = std::ranges;
7 12
8void part1(std::vector<int> A, std::vector<int> B) { 13const auto parse_input() {
9 std::sort(A.begin(), A.end()); 14 std::pair<std::vector<int>, std::vector<int>> result;
10 std::sort(B.begin(), B.end()); 15
16 for (int a, b; std::cin >> a >> b;) {
17 result.first.push_back(a);
18 result.second.push_back(b);
19 }
20
21 return result;
22}
23
24void part1(auto input) {
25 auto &[A, B] = input;
26
27 ranges::sort(A);
28 ranges::sort(B);
11 29
12 int sum = 0; 30 int sum = 0;
13 for (auto [x, y] : std::views::zip(A, B)) { 31 for (auto [x, y] : std::views::zip(A, B)) {
@@ -17,35 +35,34 @@ void part1(std::vector<int> A, std::vector<int> B) {
17 std::println("{}", sum); 35 std::println("{}", sum);
18} 36}
19 37
20void part2(std::vector<int> A, std::vector<int> B) { 38void part2(auto input) {
21 std::map<int, int> _A, _B; 39 auto &[A, B] = input;
40 std::map<int, int> freq_A, freq_B;
22 41
23 for (auto a : A) { 42 for (auto a : A) {
24 _A[a]++; 43 freq_A[a]++;
25 } 44 }
26 for (auto b : B) { 45 for (auto b : B) {
27 _B[b]++; 46 freq_B[b]++;
28 } 47 }
29 48
30 int sum = 0; 49 int sum = 0;
31 for (auto a : _A) { 50 for (auto a : freq_A) {
32 sum += a.first * a.second * _B[a.first]; 51 sum += a.first * a.second * freq_B[a.first];
33 } 52 }
34 53
35 std::println("{}", sum); 54 std::println("{}", sum);
36} 55}
37 56
38int main() { 57int main() {
39 std::vector<int> A, B; 58 const auto input = parse_input();
40
41 int a, b;
42 while (std::cin >> a >> b) {
43 A.push_back(a);
44 B.push_back(b);
45 }
46 59
47 part1(A, B); 60#ifndef NO_PART1
48 part2(A, B); 61 part1(input);
62#endif
49 63
64#ifndef NO_PART2
65 part2(input);
66#endif
50 return 0; 67 return 0;
51} 68}
diff --git a/day01/tests/test1.input b/day01/tests/test1.input
new file mode 100644
index 0000000..b8af9ad
--- /dev/null
+++ b/day01/tests/test1.input
@@ -0,0 +1,6 @@
13 4
24 3
32 5
41 3
53 9
63 3
diff --git a/day01/tests/test1.output b/day01/tests/test1.output
new file mode 100644
index 0000000..4cb4249
--- /dev/null
+++ b/day01/tests/test1.output
@@ -0,0 +1,2 @@
111
231