diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-05 08:02:45 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-05 08:02:45 +0100 |
| commit | a937350ab20adc0f8172c4d99f4d711e92dfd947 (patch) | |
| tree | 77db9835272d828adbe0ea9f9c3989dff9c9eb5c /2024 | |
| parent | 59892b65a9810044ca7f135e39c8d44094224c85 (diff) | |
| download | aoc-a937350ab20adc0f8172c4d99f4d711e92dfd947.tar.gz aoc-a937350ab20adc0f8172c4d99f4d711e92dfd947.zip | |
Day 5 2024
Diffstat (limited to '2024')
| -rw-r--r-- | 2024/05/Makefile | 24 | ||||
| -rwxr-xr-x | 2024/05/b.out | bin | 0 -> 171240 bytes | |||
| -rw-r--r-- | 2024/05/day05a.cpp | 53 | ||||
| -rw-r--r-- | 2024/05/day05b.cpp | 83 | ||||
| -rw-r--r-- | 2024/learned.txt | 1 | ||||
| -rw-r--r-- | 2024/template.cpp | 3 |
6 files changed, 163 insertions, 1 deletions
diff --git a/2024/05/Makefile b/2024/05/Makefile new file mode 100644 index 0000000..f9ca961 --- /dev/null +++ b/2024/05/Makefile | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | CC=g++ -std=c++20 -g -Wall | ||
| 2 | |||
| 3 | a: | ||
| 4 | ${CC} -o a.out day05a.cpp | ||
| 5 | |||
| 6 | b: | ||
| 7 | ${CC} -o b.out day05b.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/05/b.out b/2024/05/b.out new file mode 100755 index 0000000..6676a53 --- /dev/null +++ b/2024/05/b.out | |||
| Binary files differ | |||
diff --git a/2024/05/day05a.cpp b/2024/05/day05a.cpp new file mode 100644 index 0000000..be2037e --- /dev/null +++ b/2024/05/day05a.cpp | |||
| @@ -0,0 +1,53 @@ | |||
| 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 | vector<int> readints(string &s) { | ||
| 10 | vector<int> v; | ||
| 11 | stringstream sin(s); | ||
| 12 | int x; | ||
| 13 | |||
| 14 | while (sin >> x) | ||
| 15 | v.push_back(x); | ||
| 16 | |||
| 17 | return v; | ||
| 18 | } | ||
| 19 | |||
| 20 | bool valid(vector<int> &v, vector<pair<int, int>> &rules) { | ||
| 21 | for (unsigned i = 0; i < v.size(); i++) | ||
| 22 | for (unsigned j = i+1; j < v.size(); j++) | ||
| 23 | for (auto r : rules) | ||
| 24 | if (v[i] == r.second && v[j] == r.first) | ||
| 25 | return false; | ||
| 26 | return true; | ||
| 27 | } | ||
| 28 | |||
| 29 | int main() { | ||
| 30 | string line; | ||
| 31 | vector<pair<int, int>> rules; | ||
| 32 | while (getline(cin, line)) { | ||
| 33 | if (line == "") | ||
| 34 | break; | ||
| 35 | |||
| 36 | replace(line.begin(), line.end(), '|', ' '); | ||
| 37 | auto v = readints(line); | ||
| 38 | int x = v[0]; | ||
| 39 | int y = v[1]; | ||
| 40 | rules.push_back(make_pair(x, y)); | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | int tot = 0; | ||
| 45 | while (getline(cin, line)) { | ||
| 46 | replace(line.begin(), line.end(), ',', ' '); | ||
| 47 | auto v = readints(line); | ||
| 48 | if (valid(v, rules)) | ||
| 49 | tot += v[v.size()/2]; | ||
| 50 | } | ||
| 51 | cout << tot << endl; | ||
| 52 | return 0; | ||
| 53 | } | ||
diff --git a/2024/05/day05b.cpp b/2024/05/day05b.cpp new file mode 100644 index 0000000..d9168ac --- /dev/null +++ b/2024/05/day05b.cpp | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <iostream> | ||
| 3 | #include <sstream> | ||
| 4 | #include <string> | ||
| 5 | #include <string_view> | ||
| 6 | #include <set> | ||
| 7 | #include <vector> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | vector<int> readints(string &s) { | ||
| 11 | vector<int> v; | ||
| 12 | stringstream sin(s); | ||
| 13 | int x; | ||
| 14 | |||
| 15 | while (sin >> x) | ||
| 16 | v.push_back(x); | ||
| 17 | |||
| 18 | return v; | ||
| 19 | } | ||
| 20 | |||
| 21 | bool valid(vector<int> &v, vector<pair<int, int>> &rules) { | ||
| 22 | for (unsigned i = 0; i < v.size(); i++) | ||
| 23 | for (unsigned j = i+1; j < v.size(); j++) | ||
| 24 | for (auto r : rules) | ||
| 25 | if (v[i] == r.second && v[j] == r.first) | ||
| 26 | return false; | ||
| 27 | return true; | ||
| 28 | } | ||
| 29 | |||
| 30 | bool canbenext(int x, vector<int> &v, bool picked[], vector<pair<int, int>> &rules) { | ||
| 31 | for (unsigned i = 0; i < v.size(); i++) { | ||
| 32 | if (picked[i]) | ||
| 33 | continue; | ||
| 34 | for (auto r : rules) | ||
| 35 | if (r.first == v[i] && r.second == x) | ||
| 36 | return false; | ||
| 37 | } | ||
| 38 | |||
| 39 | return true; | ||
| 40 | } | ||
| 41 | |||
| 42 | vector<int> sort(vector<int> &v, vector<pair<int, int>> &rules) { | ||
| 43 | vector<int> w; | ||
| 44 | bool picked[v.size()] = { 0 }; | ||
| 45 | |||
| 46 | while (w.size() < v.size()) { | ||
| 47 | for (unsigned i = 0; i < v.size(); i++) { | ||
| 48 | if (!picked[i] && canbenext(v[i], v, picked, rules)) { | ||
| 49 | picked[i] = true; | ||
| 50 | w.push_back(v[i]); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | return w; | ||
| 56 | } | ||
| 57 | |||
| 58 | int main() { | ||
| 59 | string line; | ||
| 60 | vector<pair<int, int>> rules; | ||
| 61 | while (getline(cin, line)) { | ||
| 62 | if (line == "") | ||
| 63 | break; | ||
| 64 | |||
| 65 | replace(line.begin(), line.end(), '|', ' '); | ||
| 66 | auto v = readints(line); | ||
| 67 | int x = v[0]; | ||
| 68 | int y = v[1]; | ||
| 69 | rules.push_back(make_pair(x, y)); | ||
| 70 | } | ||
| 71 | |||
| 72 | int tot = 0; | ||
| 73 | while (getline(cin, line)) { | ||
| 74 | replace(line.begin(), line.end(), ',', ' '); | ||
| 75 | auto v = readints(line); | ||
| 76 | if (!valid(v, rules)) { | ||
| 77 | auto w = sort(v, rules); | ||
| 78 | tot += w[w.size()/2]; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | cout << tot << endl; | ||
| 82 | return 0; | ||
| 83 | } | ||
diff --git a/2024/learned.txt b/2024/learned.txt index 4ab29b1..3a01fc0 100644 --- a/2024/learned.txt +++ b/2024/learned.txt | |||
| @@ -5,3 +5,4 @@ List of things I learned (or refreshed) with this year's AoC. | |||
| 5 | * Day 3: std::string_view | 5 | * Day 3: std::string_view |
| 6 | * Day 4: nothing special, some practice with classic and a weird trick | 6 | * Day 4: nothing special, some practice with classic and a weird trick |
| 7 | to assign two variables at once with a pair of references pair<int&, int&> | 7 | to assign two variables at once with a pair of references pair<int&, int&> |
| 8 | * Day 5: std::find and std::replace | ||
diff --git a/2024/template.cpp b/2024/template.cpp index 3707052..7508a23 100644 --- a/2024/template.cpp +++ b/2024/template.cpp | |||
| @@ -7,7 +7,8 @@ | |||
| 7 | using namespace std; | 7 | using namespace std; |
| 8 | 8 | ||
| 9 | int main() { | 9 | int main() { |
| 10 | while (cin >> TODO) | 10 | string line; |
| 11 | while (getline(cin, line)) | ||
| 11 | ; | 12 | ; |
| 12 | return 0; | 13 | return 0; |
| 13 | } | 14 | } |
