diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-08-27 04:18:43 +0300 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-09-01 04:51:37 +0300 |
| commit | e0879b1fc2608befa6eee4dd8c2a82ade94e94ce (patch) | |
| tree | 5de27d72fa3131477b0b08815a8b663b47f022b4 | |
| parent | Add ‘skel/‘ as template directory for each day (diff) | |
| download | aoc24-e0879b1fc2608befa6eee4dd8c2a82ade94e94ce.tar.gz aoc24-e0879b1fc2608befa6eee4dd8c2a82ade94e94ce.zip | |
day01/: rewrite solution to be in line with the template
| -rw-r--r-- | day01/Makefile | 14 | ||||
| -rw-r--r-- | day01/solution.cpp | 53 | ||||
| -rw-r--r-- | day01/tests/test1.input | 6 | ||||
| -rw-r--r-- | day01/tests/test1.output | 2 |
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 @@ | |||
| 1 | CXX := g++ | include ../aoc.mk | |
| 2 | CXX_FLAGS := -std=c++23 | ||
| 3 | SOLUTION := solution.cpp | ||
| 4 | INPUT := input.txt | ||
| 5 | TARGET := solution.out | ||
| 6 | |||
| 7 | all: compile run | ||
| 8 | |||
| 9 | compile: | ||
| 10 | ${CXX} ${CXX_FLAGS} ${SOLUTION} -o ${TARGET} | ||
| 11 | |||
| 12 | run: 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 | |||
| 11 | namespace ranges = std::ranges; | ||
| 7 | 12 | ||
| 8 | void part1(std::vector<int> A, std::vector<int> B) { | 13 | const 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 | |||
| 24 | void 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 | ||
| 20 | void part2(std::vector<int> A, std::vector<int> B) { | 38 | void 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 | ||
| 38 | int main() { | 57 | int 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 @@ | |||
| 1 | 3 4 | ||
| 2 | 4 3 | ||
| 3 | 2 5 | ||
| 4 | 1 3 | ||
| 5 | 3 9 | ||
| 6 | 3 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 @@ | |||
| 1 | 11 | ||
| 2 | 31 | ||
