aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-19 06:32:23 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-19 06:32:23 +0100
commit2882d009b115657afad463fadaa103ea54a920d8 (patch)
tree114aee94e545725badec9d341fc1303f2b58d6c5
parent6bf92f2a2517f8eebddbdb139a877dcffd381edc (diff)
downloadaoc-2882d009b115657afad463fadaa103ea54a920d8.tar.gz
aoc-2882d009b115657afad463fadaa103ea54a920d8.zip
Day 19 2024
-rw-r--r--2024/19/.day19b.cpp.swpbin0 -> 12288 bytes
-rw-r--r--2024/19/Makefile24
-rw-r--r--2024/19/day19a.cpp54
-rw-r--r--2024/19/day19b.cpp54
4 files changed, 132 insertions, 0 deletions
diff --git a/2024/19/.day19b.cpp.swp b/2024/19/.day19b.cpp.swp
new file mode 100644
index 0000000..c95c362
--- /dev/null
+++ b/2024/19/.day19b.cpp.swp
Binary files differ
diff --git a/2024/19/Makefile b/2024/19/Makefile
new file mode 100644
index 0000000..53608a6
--- /dev/null
+++ b/2024/19/Makefile
@@ -0,0 +1,24 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day19a.cpp
5
6b:
7 ${CC} -o b.out day19b.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/19/day19a.cpp b/2024/19/day19a.cpp
new file mode 100644
index 0000000..28fb684
--- /dev/null
+++ b/2024/19/day19a.cpp
@@ -0,0 +1,54 @@
1#include <algorithm>
2#include <cstdint>
3#include <iostream>
4#include <map>
5#include <queue>
6#include <ranges>
7#include <set>
8#include <sstream>
9#include <string>
10#include <string_view>
11#include <vector>
12using namespace std;
13
14vector<string> gettowels(const string& line) {
15 vector<string> ret;
16 int j = 0;
17 for (int i = 0; j != -1; i = j+2) {
18 j = line.find(",", i);
19 ret.push_back(line.substr(i, (j == -1 ? line.size() : j)-i));
20 }
21 return ret;
22}
23
24bool ispossible(const string& p, const vector<string>& towels, vector<int>& mem) {
25 if (p.size() == 0)
26 return true;
27 if (mem[p.size()] != -1)
28 return mem[p.size()];
29 for (auto t : towels) {
30 if (t == p.substr(0, t.size()))
31 if((mem[p.size()] = ispossible(p.substr(
32 t.size(), p.size() - t.size()), towels, mem)))
33 return true;
34 }
35 return (mem[p.size()] = false);
36}
37
38int main() {
39 string line;
40 getline(cin, line);
41 auto towels = gettowels(line);
42 getline(cin, line);
43 vector<string> patterns;
44 while (getline(cin, line))
45 patterns.push_back(line);
46 vector<int> mem(1000);
47 int count = 0;
48 for (auto p : patterns) {
49 for (auto& m : mem) m = -1;
50 count += ispossible(p, towels, mem);
51 }
52 cout << count << endl;
53 return 0;
54}
diff --git a/2024/19/day19b.cpp b/2024/19/day19b.cpp
new file mode 100644
index 0000000..6f8b8f4
--- /dev/null
+++ b/2024/19/day19b.cpp
@@ -0,0 +1,54 @@
1#include <algorithm>
2#include <cstdint>
3#include <iostream>
4#include <map>
5#include <queue>
6#include <ranges>
7#include <set>
8#include <sstream>
9#include <string>
10#include <string_view>
11#include <vector>
12using namespace std;
13
14vector<string> gettowels(const string& line) {
15 vector<string> ret;
16 int j = 0;
17 for (int i = 0; j != -1; i = j+2) {
18 j = line.find(",", i);
19 ret.push_back(line.substr(i, (j == -1 ? line.size() : j)-i));
20 }
21 return ret;
22}
23
24int64_t countpossible(const string& p, const vector<string>& towels, vector<int64_t>& mem) {
25 if (p.size() == 0)
26 return 1;
27 if (mem[p.size()] != -1)
28 return mem[p.size()];
29 int64_t ret = 0;
30 for (auto t : towels) {
31 if (t == p.substr(0, t.size()))
32 ret += countpossible(p.substr(
33 t.size(), p.size() - t.size()), towels, mem);
34 }
35 return (mem[p.size()] = ret);
36}
37
38int main() {
39 string line;
40 getline(cin, line);
41 auto towels = gettowels(line);
42 getline(cin, line);
43 vector<string> patterns;
44 while (getline(cin, line))
45 patterns.push_back(line);
46 vector<int64_t> mem(1000);
47 int64_t count = 0;
48 for (auto p : patterns) {
49 for (auto& m : mem) m = -1;
50 count += countpossible(p, towels, mem);
51 }
52 cout << count << endl;
53 return 0;
54}

Generated with cgit - Back to sebastiano.tronto.net