commit 4aebede600c180b51d0b6ded0e42ec8a4223283e
parent 5a23d15ff9bb69d9e9b52e543596e75af3b090f9
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Tue, 3 Dec 2024 07:56:46 +0100
Added day 3 (bleah)
Diffstat:
6 files changed, 141 insertions(+), 1 deletion(-)
diff --git a/2024/03/Makefile b/2024/03/Makefile
@@ -0,0 +1,24 @@
+CC=g++ -std=c++20 -g -Wall
+
+a:
+ ${CC} -o a.out day03a.cpp
+
+b:
+ ${CC} -o b.out day03b.cpp
+
+clean:
+ rm -f a b
+
+atest: a
+ ./a.out
+
+btest: b
+ ./b.out
+
+arun: a
+ ./a.out < input
+
+brun: b
+ ./b.out < input
+
+.PHONY: a b clean atest btest arun brun
diff --git a/2024/03/b.out b/2024/03/b.out
Binary files differ.
diff --git a/2024/03/day03a.cpp b/2024/03/day03a.cpp
@@ -0,0 +1,48 @@
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <string_view>
+#include <vector>
+using namespace std;
+
+int trymult(const string_view &s) {
+ if (s.substr(0, 4) != "mul(")
+ return 0;
+
+ auto t = s.substr(4, s.length()-4);
+
+ int x = 0, y = 0;
+ int i = 0;
+ for (; i < 3; i++)
+ if (t[i] >= '0' && t[i] <= '9')
+ x = (x*10) + t[i]-'0';
+ else break;
+ if (i == 0 || t[i] != ',')
+ return 0;
+ i++;
+ int j = 0;
+ for (; j < 3; j++)
+ if (t[i+j] >= '0' && t[i+j] <= '9')
+ y = (y*10) + t[i+j]-'0';
+ else break;
+ if (j == 0 || t[j+i] != ')')
+ return 0;
+ return x*y;
+}
+
+int mults(const string &line) {
+ int result = 0;
+ for (auto i = line.begin(); i != line.end(); i++)
+ result += trymult(string_view(i, line.end()));
+ return result;
+}
+
+int main() {
+ string line;
+ int result = 0;
+ while (cin >> line)
+ result += mults(line);
+ cout << result << endl;
+ return 0;
+}
diff --git a/2024/03/day03b.cpp b/2024/03/day03b.cpp
@@ -0,0 +1,63 @@
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <string_view>
+#include <vector>
+using namespace std;
+
+bool state = true;
+
+int trymult(const string_view &s) {
+ if (s.substr(0, 4) != "mul(")
+ return 0;
+
+ auto t = s.substr(4, s.length()-4);
+
+ int x = 0, y = 0;
+ int i = 0;
+ for (; i < 3; i++)
+ if (t[i] >= '0' && t[i] <= '9')
+ x = (x*10) + t[i]-'0';
+ else break;
+ if (i == 0 || t[i] != ',')
+ return 0;
+ i++;
+ int j = 0;
+ for (; j < 3; j++)
+ if (t[i+j] >= '0' && t[i+j] <= '9')
+ y = (y*10) + t[i+j]-'0';
+ else break;
+ if (j == 0 || t[j+i] != ')')
+ return 0;
+
+ return x*y;
+}
+
+bool trytogglestate(bool current_state, const string_view &s) {
+ if (s.substr(0, 4) == "do()")
+ return true;
+ if (s.substr(0, 7) == "don't()")
+ return false;
+ return current_state;
+}
+
+int mults(const string &line) {
+ int result = 0;
+ for (auto i = line.begin(); i != line.end(); i++) {
+ string_view s(i, line.end());
+ state = trytogglestate(state, s);
+ if (state)
+ result += trymult(s);
+ }
+ return result;
+}
+
+int main() {
+ string line;
+ int result = 0;
+ while (cin >> line)
+ result += mults(line);
+ cout << result << endl;
+ return 0;
+}
diff --git a/2024/learned.txt b/2024/learned.txt
@@ -1,3 +1,5 @@
List of things I learned (or refreshed) with this year's AoC.
* Day 1: std::count()
+* Day 2: std::sstream
+* Day 3: std::string_view
diff --git a/2024/template.cpp b/2024/template.cpp
@@ -1,6 +1,9 @@
+#include <algorithm>
#include <iostream>
+#include <sstream>
+#include <string>
+#include <string_view>
#include <vector>
-#include <algorithm>
using namespace std;
int main() {