From 4aebede600c180b51d0b6ded0e42ec8a4223283e Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 3 Dec 2024 07:56:46 +0100 Subject: Added day 3 (bleah) --- 2024/03/Makefile | 24 ++++++++++++++++++++ 2024/03/b.out | Bin 0 -> 103160 bytes 2024/03/day03a.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ 2024/03/day03b.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2024/learned.txt | 2 ++ 2024/template.cpp | 5 ++++- 6 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 2024/03/Makefile create mode 100755 2024/03/b.out create mode 100644 2024/03/day03a.cpp create mode 100644 2024/03/day03b.cpp 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 @@ +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 new file mode 100755 index 0000000..49b7a0c Binary files /dev/null and b/2024/03/b.out 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 @@ +#include +#include +#include +#include +#include +#include +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 new file mode 100644 index 0000000..06c8f21 --- /dev/null +++ b/2024/03/day03b.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +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 index 56101dd..63a0a71 100644 --- 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 index 8428266..3707052 100644 --- a/2024/template.cpp +++ b/2024/template.cpp @@ -1,6 +1,9 @@ +#include #include +#include +#include +#include #include -#include using namespace std; int main() { -- cgit v1.3