aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
Diffstat (limited to '2024')
-rw-r--r--2024/03/Makefile24
-rwxr-xr-x2024/03/b.outbin0 -> 103160 bytes
-rw-r--r--2024/03/day03a.cpp48
-rw-r--r--2024/03/day03b.cpp63
-rw-r--r--2024/learned.txt2
-rw-r--r--2024/template.cpp5
6 files changed, 141 insertions, 1 deletions
diff --git a/2024/03/Makefile b/2024/03/Makefile
new file mode 100644
index 0000000..103a6a9
--- /dev/null
+++ b/2024/03/Makefile
@@ -0,0 +1,24 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day03a.cpp
5
6b:
7 ${CC} -o b.out day03b.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/03/b.out b/2024/03/b.out
new file mode 100755
index 0000000..49b7a0c
--- /dev/null
+++ b/2024/03/b.out
Binary files differ
diff --git a/2024/03/day03a.cpp b/2024/03/day03a.cpp
new file mode 100644
index 0000000..2fb04cf
--- /dev/null
+++ b/2024/03/day03a.cpp
@@ -0,0 +1,48 @@
1#include <algorithm>
2#include <iostream>
3#include <sstream>
4#include <string>
5#include <string_view>
6#include <vector>
7using namespace std;
8
9int trymult(const string_view &s) {
10 if (s.substr(0, 4) != "mul(")
11 return 0;
12
13 auto t = s.substr(4, s.length()-4);
14
15 int x = 0, y = 0;
16 int i = 0;
17 for (; i < 3; i++)
18 if (t[i] >= '0' && t[i] <= '9')
19 x = (x*10) + t[i]-'0';
20 else break;
21 if (i == 0 || t[i] != ',')
22 return 0;
23 i++;
24 int j = 0;
25 for (; j < 3; j++)
26 if (t[i+j] >= '0' && t[i+j] <= '9')
27 y = (y*10) + t[i+j]-'0';
28 else break;
29 if (j == 0 || t[j+i] != ')')
30 return 0;
31 return x*y;
32}
33
34int mults(const string &line) {
35 int result = 0;
36 for (auto i = line.begin(); i != line.end(); i++)
37 result += trymult(string_view(i, line.end()));
38 return result;
39}
40
41int main() {
42 string line;
43 int result = 0;
44 while (cin >> line)
45 result += mults(line);
46 cout << result << endl;
47 return 0;
48}
diff --git a/2024/03/day03b.cpp b/2024/03/day03b.cpp
new file mode 100644
index 0000000..06c8f21
--- /dev/null
+++ b/2024/03/day03b.cpp
@@ -0,0 +1,63 @@
1#include <algorithm>
2#include <iostream>
3#include <sstream>
4#include <string>
5#include <string_view>
6#include <vector>
7using namespace std;
8
9bool state = true;
10
11int trymult(const string_view &s) {
12 if (s.substr(0, 4) != "mul(")
13 return 0;
14
15 auto t = s.substr(4, s.length()-4);
16
17 int x = 0, y = 0;
18 int i = 0;
19 for (; i < 3; i++)
20 if (t[i] >= '0' && t[i] <= '9')
21 x = (x*10) + t[i]-'0';
22 else break;
23 if (i == 0 || t[i] != ',')
24 return 0;
25 i++;
26 int j = 0;
27 for (; j < 3; j++)
28 if (t[i+j] >= '0' && t[i+j] <= '9')
29 y = (y*10) + t[i+j]-'0';
30 else break;
31 if (j == 0 || t[j+i] != ')')
32 return 0;
33
34 return x*y;
35}
36
37bool trytogglestate(bool current_state, const string_view &s) {
38 if (s.substr(0, 4) == "do()")
39 return true;
40 if (s.substr(0, 7) == "don't()")
41 return false;
42 return current_state;
43}
44
45int mults(const string &line) {
46 int result = 0;
47 for (auto i = line.begin(); i != line.end(); i++) {
48 string_view s(i, line.end());
49 state = trytogglestate(state, s);
50 if (state)
51 result += trymult(s);
52 }
53 return result;
54}
55
56int main() {
57 string line;
58 int result = 0;
59 while (cin >> line)
60 result += mults(line);
61 cout << result << endl;
62 return 0;
63}
diff --git a/2024/learned.txt b/2024/learned.txt
index 56101dd..63a0a71 100644
--- a/2024/learned.txt
+++ b/2024/learned.txt
@@ -1,3 +1,5 @@
1List of things I learned (or refreshed) with this year's AoC. 1List of things I learned (or refreshed) with this year's AoC.
2 2
3* Day 1: std::count() 3* Day 1: std::count()
4* Day 2: std::sstream
5* Day 3: std::string_view
diff --git a/2024/template.cpp b/2024/template.cpp
index 8428266..3707052 100644
--- a/2024/template.cpp
+++ b/2024/template.cpp
@@ -1,6 +1,9 @@
1#include <algorithm>
1#include <iostream> 2#include <iostream>
3#include <sstream>
4#include <string>
5#include <string_view>
2#include <vector> 6#include <vector>
3#include <algorithm>
4using namespace std; 7using namespace std;
5 8
6int main() { 9int main() {

Generated with cgit - Back to sebastiano.tronto.net