diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-02 06:46:28 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-02 06:46:28 +0100 |
| commit | 5a23d15ff9bb69d9e9b52e543596e75af3b090f9 (patch) | |
| tree | c89e8120d48962c77dd74144616a8b6982f8822b | |
| parent | 0aefd410a18acf826af5187cdb038fd9b4f25fc5 (diff) | |
| download | aoc-5a23d15ff9bb69d9e9b52e543596e75af3b090f9.tar.gz aoc-5a23d15ff9bb69d9e9b52e543596e75af3b090f9.zip | |
Day 2 2024
| -rw-r--r-- | 2024/02/Makefile | 24 | ||||
| -rwxr-xr-x | 2024/02/b.out | bin | 0 -> 152640 bytes | |||
| -rw-r--r-- | 2024/02/day02a.cpp | 30 | ||||
| -rw-r--r-- | 2024/02/day02b.cpp | 48 |
4 files changed, 102 insertions, 0 deletions
diff --git a/2024/02/Makefile b/2024/02/Makefile new file mode 100644 index 0000000..c3cdac5 --- /dev/null +++ b/2024/02/Makefile | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | CC=g++ -std=c++20 -g -Wall | ||
| 2 | |||
| 3 | a: | ||
| 4 | ${CC} -o a.out day02a.cpp | ||
| 5 | |||
| 6 | b: | ||
| 7 | ${CC} -o b.out day02b.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/02/b.out b/2024/02/b.out new file mode 100755 index 0000000..fc29aef --- /dev/null +++ b/2024/02/b.out | |||
| Binary files differ | |||
diff --git a/2024/02/day02a.cpp b/2024/02/day02a.cpp new file mode 100644 index 0000000..cdd9efa --- /dev/null +++ b/2024/02/day02a.cpp | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <sstream> | ||
| 3 | #include <vector> | ||
| 4 | #include <algorithm> | ||
| 5 | #include <string> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | bool isvalid(const string &line) { | ||
| 9 | int x, y, b; | ||
| 10 | istringstream s(line); | ||
| 11 | |||
| 12 | s >> x >> y; | ||
| 13 | b = x > y ? 1 : -1; | ||
| 14 | do { | ||
| 15 | int d = b * (x-y); | ||
| 16 | if (d < 1 || d > 3) | ||
| 17 | return false; | ||
| 18 | x = y; | ||
| 19 | } while (s >> y); | ||
| 20 | |||
| 21 | return true; | ||
| 22 | } | ||
| 23 | |||
| 24 | int main() { | ||
| 25 | int tot = 0; | ||
| 26 | for (string line; getline(cin, line); ) | ||
| 27 | tot += isvalid(line); | ||
| 28 | cout << tot << endl; | ||
| 29 | return 0; | ||
| 30 | } | ||
diff --git a/2024/02/day02b.cpp b/2024/02/day02b.cpp new file mode 100644 index 0000000..2e3dcf4 --- /dev/null +++ b/2024/02/day02b.cpp | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <sstream> | ||
| 3 | #include <vector> | ||
| 4 | #include <algorithm> | ||
| 5 | #include <string> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | bool isvalid_v(const vector<int> &v) { | ||
| 9 | int b = v[0] > v[1] ? 1 : -1; | ||
| 10 | for (unsigned i = 0; i < v.size()-1; i++) { | ||
| 11 | int d = b * (v[i] - v[i+1]); | ||
| 12 | if (d < 1 || d > 3) | ||
| 13 | return false; | ||
| 14 | } | ||
| 15 | return true; | ||
| 16 | } | ||
| 17 | |||
| 18 | vector<int> getv(const string &line) { | ||
| 19 | int x; | ||
| 20 | vector<int> ret; | ||
| 21 | istringstream s(line); | ||
| 22 | |||
| 23 | while(s >> x) | ||
| 24 | ret.push_back(x); | ||
| 25 | |||
| 26 | return ret; | ||
| 27 | } | ||
| 28 | |||
| 29 | bool isvalid(const string &line) { | ||
| 30 | vector v0 = getv(line); | ||
| 31 | if (isvalid_v(v0)) | ||
| 32 | return true; | ||
| 33 | for (unsigned i = 0; i < v0.size(); i++) { | ||
| 34 | vector v = v0; | ||
| 35 | v.erase(v.begin() + i); | ||
| 36 | if (isvalid_v(v)) | ||
| 37 | return true; | ||
| 38 | } | ||
| 39 | return false; | ||
| 40 | } | ||
| 41 | |||
| 42 | int main() { | ||
| 43 | int tot = 0; | ||
| 44 | for (string line; getline(cin, line); ) | ||
| 45 | tot += isvalid(line); | ||
| 46 | cout << tot << endl; | ||
| 47 | return 0; | ||
| 48 | } | ||
