aboutsummaryrefslogtreecommitdiff
path: root/2024/19/day19a.cpp
blob: 28fb684d3d118fc7a467e8e46c9bc1ed7a7b5217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>
#include <queue>
#include <ranges>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
using namespace std;

vector<string> gettowels(const string& line) {
	vector<string> ret;
	int j = 0;
	for (int i = 0; j != -1; i = j+2) {
		j = line.find(",", i);
		ret.push_back(line.substr(i, (j == -1 ? line.size() : j)-i));
	}
	return ret;
}

bool ispossible(const string& p, const vector<string>& towels, vector<int>& mem) {
	if (p.size() == 0)
		return true;
	if (mem[p.size()] != -1)
		return mem[p.size()];
	for (auto t : towels) {
		if (t == p.substr(0, t.size()))
			if((mem[p.size()] = ispossible(p.substr(
			    t.size(), p.size() - t.size()), towels, mem)))
				return true;
	}
	return (mem[p.size()] = false);
}

int main() {
	string line;
	getline(cin, line);
	auto towels = gettowels(line);
	getline(cin, line);
	vector<string> patterns;
	while (getline(cin, line))
		patterns.push_back(line);
	vector<int> mem(1000);
	int count = 0;
	for (auto p : patterns) {
		for (auto& m : mem) m = -1;
		count += ispossible(p, towels, mem);
	}
	cout << count << endl;
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net