diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-03 07:56:46 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-03 07:56:46 +0100 |
| commit | 4aebede600c180b51d0b6ded0e42ec8a4223283e (patch) | |
| tree | 09547476e925ec7e298888d5df9c4d21c80d2f95 /2024/03 | |
| parent | 5a23d15ff9bb69d9e9b52e543596e75af3b090f9 (diff) | |
| download | aoc-4aebede600c180b51d0b6ded0e42ec8a4223283e.tar.gz aoc-4aebede600c180b51d0b6ded0e42ec8a4223283e.zip | |
Added day 3 (bleah)
Diffstat (limited to '2024/03')
| -rw-r--r-- | 2024/03/Makefile | 24 | ||||
| -rwxr-xr-x | 2024/03/b.out | bin | 0 -> 103160 bytes | |||
| -rw-r--r-- | 2024/03/day03a.cpp | 48 | ||||
| -rw-r--r-- | 2024/03/day03b.cpp | 63 |
4 files changed, 135 insertions, 0 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 @@ | |||
| 1 | CC=g++ -std=c++20 -g -Wall | ||
| 2 | |||
| 3 | a: | ||
| 4 | ${CC} -o a.out day03a.cpp | ||
| 5 | |||
| 6 | b: | ||
| 7 | ${CC} -o b.out day03b.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/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> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | int 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 | |||
| 34 | int 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 | |||
| 41 | int 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> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | bool state = true; | ||
| 10 | |||
| 11 | int 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 | |||
| 37 | bool 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 | |||
| 45 | int 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 | |||
| 56 | int main() { | ||
| 57 | string line; | ||
| 58 | int result = 0; | ||
| 59 | while (cin >> line) | ||
| 60 | result += mults(line); | ||
| 61 | cout << result << endl; | ||
| 62 | return 0; | ||
| 63 | } | ||
