diff options
Diffstat (limited to '2024/01')
| -rw-r--r-- | 2024/01/Makefile | 24 | ||||
| -rwxr-xr-x | 2024/01/b.out | bin | 0 -> 78536 bytes | |||
| -rw-r--r-- | 2024/01/day01a.cpp | 24 | ||||
| -rw-r--r-- | 2024/01/day01b.cpp | 21 |
4 files changed, 69 insertions, 0 deletions
diff --git a/2024/01/Makefile b/2024/01/Makefile new file mode 100644 index 0000000..706085c --- /dev/null +++ b/2024/01/Makefile | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | CC=g++ -std=c++20 -g -Wall | ||
| 2 | |||
| 3 | a: | ||
| 4 | ${CC} -o a.out day01a.cpp | ||
| 5 | |||
| 6 | b: | ||
| 7 | ${CC} -o b.out day01b.cpp | ||
| 8 | |||
| 9 | clean: | ||
| 10 | rm -f a b | ||
| 11 | |||
| 12 | atest: a | ||
| 13 | ./a.out | ||
| 14 | |||
| 15 | btest: b | ||
| 16 | ./b.out | ||
| 17 | |||
| 18 | arun: a | ||
| 19 | ./a.out < input | ||
| 20 | |||
| 21 | brun: b | ||
| 22 | ./b.out < input | ||
| 23 | |||
| 24 | .PHONY: a b clean atest btest arun brun | ||
diff --git a/2024/01/b.out b/2024/01/b.out new file mode 100755 index 0000000..3b75578 --- /dev/null +++ b/2024/01/b.out | |||
| Binary files differ | |||
diff --git a/2024/01/day01a.cpp b/2024/01/day01a.cpp new file mode 100644 index 0000000..e4a61e0 --- /dev/null +++ b/2024/01/day01a.cpp | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <algorithm> | ||
| 3 | #include <vector> | ||
| 4 | using namespace std; | ||
| 5 | |||
| 6 | int main() { | ||
| 7 | int x, y, tot; | ||
| 8 | vector<int> v, w; | ||
| 9 | |||
| 10 | while (cin >> x >> y) { | ||
| 11 | v.push_back(x); | ||
| 12 | w.push_back(y); | ||
| 13 | } | ||
| 14 | |||
| 15 | sort(v.begin(), v.end()); | ||
| 16 | sort(w.begin(), w.end()); | ||
| 17 | |||
| 18 | tot = 0; | ||
| 19 | for (unsigned i = 0; i < v.size(); i++) | ||
| 20 | tot += abs(v[i] - w[i]); | ||
| 21 | |||
| 22 | cout << tot << endl; | ||
| 23 | return 0; | ||
| 24 | } | ||
diff --git a/2024/01/day01b.cpp b/2024/01/day01b.cpp new file mode 100644 index 0000000..91ef0db --- /dev/null +++ b/2024/01/day01b.cpp | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <algorithm> | ||
| 3 | #include <vector> | ||
| 4 | using namespace std; | ||
| 5 | |||
| 6 | int main() { | ||
| 7 | int x, y, tot; | ||
| 8 | vector<int> v, w; | ||
| 9 | |||
| 10 | while (cin >> x >> y) { | ||
| 11 | v.push_back(x); | ||
| 12 | w.push_back(y); | ||
| 13 | } | ||
| 14 | |||
| 15 | tot = 0; | ||
| 16 | for (unsigned i = 0; i < v.size(); i++) | ||
| 17 | tot += v[i] * count(w.begin(), w.end(), v[i]); | ||
| 18 | |||
| 19 | cout << tot << endl; | ||
| 20 | return 0; | ||
| 21 | } | ||
