aboutsummaryrefslogtreecommitdiff
path: root/2024/02
diff options
context:
space:
mode:
Diffstat (limited to '2024/02')
-rw-r--r--2024/02/Makefile24
-rwxr-xr-x2024/02/b.outbin0 -> 152640 bytes
-rw-r--r--2024/02/day02a.cpp30
-rw-r--r--2024/02/day02b.cpp48
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 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day02a.cpp
5
6b:
7 ${CC} -o b.out day02b.cpp
8
9clean:
10 rm -f a b
11
12atest: a
13 ./a.out
14
15btest: b
16 ./b.out
17
18arun: a
19 ./a.out < input
20
21brun: 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>
6using namespace std;
7
8bool 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
24int 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>
6using namespace std;
7
8bool 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
18vector<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
29bool 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
42int main() {
43 int tot = 0;
44 for (string line; getline(cin, line); )
45 tot += isvalid(line);
46 cout << tot << endl;
47 return 0;
48}

Generated with cgit - Back to sebastiano.tronto.net